当前位置: 代码迷 >> J2ME >> java.lang.NullPointerException: 
  详细解决方案

java.lang.NullPointerException: 

热度:8977   发布时间:2013-02-25 21:38:42.0
j2me新手问题
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

public class gamestart extends MIDlet {


protected void destroyApp(boolean arg0) {
// TODO Auto-generated method stub

}

protected void pauseApp() {
// TODO Auto-generated method stub

}

protected void startApp() {
Display.getDisplay(this).setCurrent(new myCanvas());// TODO Auto-generated method stub

}

}


import javax.microedition.lcdui.*;

public class myCanvas extends Canvas implements Runnable {
public Image ims[];
public int nowFrame=0;
public int px;
public myCanvas()
{
ims=new Image[4];
try{
ims[0]=Image.createImage("/bird1.png");
ims[1]=Image.createImage("/bird2.png");
ims[2]=Image.createImage("/bird3.png");
ims[3]=Image.createImage("/bird4.png");
}catch(Exception e){}
Thread t=new Thread(this);
t.start();
}

public void run() {
while(true)
{
px-=8;
if(px<0){px=240;}
nowFrame++;
if(nowFrame>3){nowFrame=0;}
this.repaint();
try{Thread.sleep(80);}
catch(Exception e){}
}
// TODO Auto-generated method stub

}

protected void paint(Graphics g) {
g.setColor(0xFFFFFF);
g.fillRect(0, 0, 240,289);
g.drawImage(ims[nowFrame],px,30,0);// TODO Auto-generated method stub

}

}
它运行时手机画面闪一下就不见了,console出现这些Running with storage root C:\Users\Administrator\j2mewtk\2.5.2\appdb\DefaultColorPhone
Running with locale: Chinese (Simplified)_People's Republic of China.936
Running in the identified_third_party security domain
java.lang.NullPointerException
at myCanvas.paint(+38)
at javax.microedition.lcdui.Canvas.callPaint(+85)
at javax.microedition.lcdui.Display.repaint(+82)
at javax.microedition.lcdui.Display.registerNewCurrent(+235)
at javax.microedition.lcdui.Display.access$700(+6)
at javax.microedition.lcdui.Display$DisplayAccessor.foregroundNotify(+46)
at javax.microedition.lcdui.Display$DisplayManagerImpl.notifyWantsForeground(+152)
at javax.microedition.lcdui.Display$DisplayManagerImpl.access$100(+6)
at javax.microedition.lcdui.Display.setCurrent(+70)
at gamestart.startApp(+14)
at javax.microedition.midlet.MIDletProxy.startApp(+7)
at com.sun.midp.midlet.Scheduler.schedule(+270)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+80)
我是新手,不知道怎么搞?毫无头绪啊。。。不知道是软件没设置好还是软件不兼容什么的,,,,

------解决方案--------------------------------------------------------
java.lang.NullPointerException: 
at myCanvas.paint(+38)
 明显的空指针异常你的myCanvas 的 paint方法(类的第38行)没有被赋值
应该 ims[nowFrame]是空的
  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!
 
  • 最近登录:Wed Dec 13 03:52:31 CST 2017
  • 最近登录:Wed Dec 13 03:52:31 CST 2017
  • 最近登录:Wed Dec 13 03:52:31 CST 2017
  • 最近登录:Wed Dec 13 03:52:31 CST 2017
  • 最近登录:Wed Dec 13 03:52:31 CST 2017