我的代码给全了啊!
我把我的程序代码和运行时抛出的异常标示如下,麻烦大家看一下,我实在是被这一类问题烦死了,谢谢大家帮忙一下吧!!!
(本程序中用到的为一张28*40像素png格式的图片)
import javax.microedition.lcdui.*;
import java.util.*;
import java.io.*;
import javax.microedition.midlet.*;
public class yuchun extends MIDlet implements CommandListener{
private Command cmd_exit;
public Display display;
mycanvas canvas;
public yuchun(){
canvas=new mycanvas(display.getDisplay(this));
}
public void startApp(){
Command cmd_exit=new Command("exit",Command.EXIT,1);
canvas.addCommand(cmd_exit);
canvas.setCommandListener(this);
canvas.start();
}
public void pauseApp(){}
public void destroyApp(boolean a){}
public void commandAction(Command c,Displayable d){
if(c==cmd_exit){
canvas.stop();
destroyApp(false);
notifyDestroyed();
}
}
}//class yuchun
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.io.*;
public class mycanvas extends GameCanvas implements Runnable{ //定义的GameCanvas一个类mycanvas
private Sprite yuchu;
private long frameDelay;
public boolean sleeping;
public int x=0,y=0;
private Display display;
private Random rand;
public mycanvas(Display d){
super(true);
display=d;
frameDelay=33;
}
public void start(){
display.setCurrent(this);
rand=new Random();
sleeping=false;
try{
yuchu=new Sprite(Image.createImage("1.png"));
}catch(IOException e){System.err.println("failure");}
yuchu.setPosition(0,0);
Thread t=new Thread(this);
t.start();
}
public void run(){
Graphics g=getGraphics();
while(!sleeping){
update();
draw(g);
try{Thread.sleep(frameDelay);
}catch(InterruptedException e){}
}
}
public void stop(){
sleeping=true;
}
public void update(){
if(rand.nextInt()%5==0){
int a=Math.max(-8,rand.nextInt()%2);
x=Math.min(a,8);
int b=Math.max(-8,rand.nextInt()%2);
y=Math.min(b,8);
yuchu.move(x,y);
}
if(yuchu.getX()<=-yuchu.getWidth()) // 此段代码为如果精灵超出了显示范围,就折回屏幕的另一端
yuchu.setPosition(getWidth(),yuchu.getY());
else if(yuchu.getX()>=getWidth())
yuchu.setPosition(-yuchu.getWidth(),yuchu.getY());
if(yuchu.getY()<=-yuchu.getHeight())
yuchu.setPosition(yuchu.getX(),getHeight());
else if(yuchu.getY()>=getHeight())
yuchu.setPosition(yuchu.getX(),-yuchu.getHeight());
}
public void draw(Graphics g){
g.setColor(0,0,0);
g.fillRect(0,0,getWidth(),getHeight());
yuchu.paint(g);
flushGraphics();
}
}//class mycanvas
以下为程序在运行过程中抛出的异常:
项目设置已保存
正在生成 "pro1"
生成完成
正在通过存储根 DefaultColorPhone 来运行
failure
startApp threw an Exception
java.lang.NullPointerException
java.lang.NullPointerException
at mycanvas.start(+58)
at yuchun.startApp(+36)
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)