package newpackage;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author kl0
* @version
*/
public class MyCartoonMidlet extends MIDlet {
public MainCanvas m_MainCanvas;
public MyCartoonMidlet(){
super();
}
public void startApp() throws MIDletStateChangeException {
m_MainCanvas=new MainCanvas();
Display.getDisplay(this).setCurrent(m_MainCanvas);
while(true){
m_MainCanvas.repaint();
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) throws MIDletStateChangeException{
}
}
package newpackage;
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
*
* @author kl0
*/
public class MainCanvas extends Canvas{
public Image m_Image;
/** Creates a new instance of MainCarvas */
public MainCanvas() {
try {
m_Image=Image.createImage( "/IM.png ");
} catch (IOException e) {
}
}
protected void paint(Graphics graphics) {
graphics.drawImage(m_Image,0,0,Graphics.TOP|Graphics.LEFT);
}
}
/*运行的时候就
java.lang.NullPointerException: at newpackage.MainCanvas.paint(MainCanvas.java:34)
at javax.microedition.lcdui.Canvas.callPaint(Canvas.java:1063)
at javax.microedition.lcdui.Display.repaint(Display.java:1435)
at javax.microedition.lcdui.Display$DisplayAccessor.repaint(Display.java:2165)
at javax.microedition.lcdui.Display$DisplayManagerImpl.repaint(Display.java:2912)
at com.sun.midp.lcdui.DefaultEventHandler.repaintScreenEvent(DefaultEventHandler.java:398)
at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(DefaultEventHandler.java:728)
怎样解决!
------解决方法--------------------------------------------------------
空指针异常,传进来的参数有可能为空,graphics未被实例化
------解决方法--------------------------------------------------------
graphics g大写,Graphics
------解决方法--------------------------------------------------------
为什么没人认为是m_Image为null呢??
我觉得是:
try {
m_Image=Image.createImage( "/IM.png ");