当前位置: 代码迷 >> J2ME >> 第一次做程序,运行空指针异常,麻烦高手帮小弟我看看
  详细解决方案

第一次做程序,运行空指针异常,麻烦高手帮小弟我看看

热度:7494   发布时间:2013-02-25 21:34:59.0
第一次做程序,运行空指针错误,麻烦高手帮我看看

在Eclipse里面做的,
  运行的时候,模拟器一闪就消失了,
有下面提示。源码在下面,麻烦了。
运行其它HelloWorld小程序时,没问题,模拟器可以正常显示
 
Running with storage root MediaControlSkin
Running with locale: Chinese_People's Republic of China.936
startApp threw an Exception
java.lang.NullPointerException
java.lang.NullPointerException
at hsnake.Hsnakemodel.<init>(+133)
at hsnake.Hsnake.startApp(+8)
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)
Execution completed.
3401665 bytecodes executed
34 thread switches
1649 classes in the system (including system classes)
17813 dynamic objects allocated (529564 bytes)
3 garbage collections (457204 bytes collected)



package hsnake;

import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.*;
import java.io.IOException;
import java.util.*;

public class Hsnakemodel extends GameCanvas implements Runnable 
{
private boolean running=false;
private Graphics g;


boolean [][]matrix;//蛇身或食物设为true,空白处设为flase
Vector nodeArray =new Vector(5);//蛇结点 数组
Node food ;//食物
public static final int nodewidth=10;//每一个结点的宽高
public static final int nodeheight=10;
public int Width;//屏幕宽高
public int Height;
private int maxX;
private int maxY;
int Direction=2;//当前前进方向,初始为UP
int newDirection;//按键改变后的方向
int timeInterval=200;//屏幕刷新间隔
boolean paused=false;
public static final int UP=2;
public static final int DOWN=4;
public static final int LEFT=1;
public static final int RIGHT=3;

public Hsnakemodel(){
super (true);
Width=getWidth();
Height=getHeight();
maxX=Width/nodewidth;
maxY=Height/nodeheight;
matrix=new boolean [maxX][];

for (int i=0;i<5;++i)
{
int x=maxX/2+i;
int y=maxY/2;
nodeArray.addElement(new Node(x,y));
matrix[x][y]=true;
}
food=creatFood();
matrix[food.x][food.y]=true;


}


public void startup()
{
this.running=true;

Thread thread =new Thread (this);
//启动线程
thread.start();
}
public void run()
{
g=getGraphics();
while(running)
{
try
{
Thread.sleep(timeInterval);
input(g);//检测键盘更新
}
catch(Exception e)
{
break;
}
if(!paused)
{
if(moveOn())
{
paint(g);//更新移动坐标后,重绘
}
else
{
break;
}
}
}
  running=false;
}


public void changeDirection(int newDirection)
{
if (Direction%2!=newDirection%2)
Direction=newDirection;
}

public void input(Graphics g)throws IOException
{
int keystates=getKeyStates();
switch (keystates)
{
case UP_PRESSED:
newDirection=UP;

break;
case DOWN_PRESSED:
newDirection=DOWN;
break;
case LEFT_PRESSED:
newDirection=LEFT;
break;
case RIGHT_PRESSED:
newDirection=RIGHT;
break;
case FIRE_PRESSED:
this.changePauseStates();
break;
}
}
public boolean moveOn()
{
Node n=(Node)nodeArray.firstElement();//取得头结点
int x=n.x;
int y=n.y;
switch (Direction)
{
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
}
if ((0<=x&&x<nodewidth*maxX)&&(0<=y&&y<nodeheight*maxY))//是否越界
{
if (matrix[x][y])
{
if (x==food.x&&y==food.y)//吃到食物
{
nodeArray.insertElementAt(food, 0);//把食物结点作为头结点
  相关解决方案