本人一个程序就是通过按方向健实现一幅图片的移动,图片为28*40像素png格式,程序简单, 可运行时抛出的异常让我郁闷不已,实在是太多了,我也被弄得无可耐何,请帮忙啊!!! import javax.microedition.lcdui.*; import java.io.*;
public class picture extends Canvas{ //定义类Canvas Image img; Display display; int x=getWidth()/2; int y=getHeight()/2; public picture(Display d){ display=d; try{ img=Image.createImage("\10.png"); }catch(IOException e){System.err.println("loading failure");}; } public void paint(Graphics g){ g.setColor(255,255,255); g.fillRect(0,0,getWidth(),getHeight()); g.drawImage(img,x,y,Graphics.HCENTER|Graphics.VCENTER); } public void keyPressed(int keyCode){ switch(getGameAction(keyCode)){ case LEFT:x-=2;repaint();break; case RIGHT:x+=2;repaint();break; case UP:y-=2;repaint();break; case DOWN:y+=2;repaint();break; } } public void start(){ display.setCurrent(this); repaint(); } }
import javax.microedition.lcdui.*; import javax.microedition.midlet.*;
public class p183 extends MIDlet{ Display display; picture mypicture; public p183(){ display=Display.getDisplay(this); mypicture=new picture(display); } public void startApp(){ mypicture.start(); } public void pauseApp(){} public void destroyApp(boolean a){} }
运行时抛出的异常如下: 生成完成 正在通过存储根 DefaultColorPhone 来运行 loading failure java.lang.NullPointerException: at picture.paint(+44) at javax.microedition.lcdui.Canvas.callPaint(+80) at javax.microedition.lcdui.Display.repaint(+77) at javax.microedition.lcdui.Display.registerNewCurrent(+237) at javax.microedition.lcdui.Display.access$400(+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.setCurrentImpl(+115) at javax.microedition.lcdui.Display.setCurrent(+29) at picture.start(+8) at p183.startApp(+7) 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(+116) java.lang.NullPointerException: at picture.paint(+44) at javax.microedition.lcdui.Canvas.callPaint(+80) at javax.microedition.lcdui.Display.repaint(+77) at javax.microedition.lcdui.Display$DisplayAccessor.repaint(+14) at javax.microedition.lcdui.Display$DisplayManagerImpl.repaint(+16) at com.sun.midp.lcdui.DefaultEventHandler.repaintScreenEvent(+24) at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+774) 请大家帮下忙啊,非常感谢!!!