首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

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

2012-01-13 
第一次做程序,运行空指针错误,麻烦高手帮我看看在Eclipse里面做的,  运行的时候,模拟器一闪就消失了,有下

第一次做程序,运行空指针错误,麻烦高手帮我看看

在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);//把食物结点作为头结点


food=creatFood();//产生新的食物
matrix[food.x][food.y]=true;//
return true;
}
else return false;
}
else//未吃到食物,一般移动
{
nodeArray.insertElementAt(new Node(x,y), 0);//把当前位置坐标形成的结点,作为头结点
matrix[x][y]=true;
int m=nodeArray.size();//得到蛇身数组的长度
n=(Node)nodeArray.elementAt(m-1);//把蛇尾传递给n
nodeArray.removeElementAt(m-1);//移除蛇尾
  matrix[n.x][n.y]=false;
  return true;
}
}
return false;//如果越界,则返回false 游戏结束

}


public void paint(Graphics g)
{
g.setColor(0x000000);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xffffff);
g.drawLine(nodewidth*maxX+1, 0, 0, nodeheight*maxY+1);
int m=nodeArray.size();
for (int i=0;i<m;i++)
{
Node n=(Node)nodeArray.elementAt(i);
drawNode(g,n);
}
g.setColor(0xff0000);
drawNode(g,food);
flushGraphics();
}
private void drawNode(Graphics g,Node n)
{
  g.drawRect(n.x, n.y, Width, Height);

}
private Node creatFood()
{
int x=0;
int y=0;
do{
Random r=new Random();
x=r.nextInt(maxX);
y=r.nextInt(maxY);
}while (matrix[x][y]);
return new Node(x,y);
}
private void changePauseStates()
{
paused=!paused;

}
}//类结束



[解决办法]
你看看是在哪行报的空指针
[解决办法]
matrix的创建有问题
[解决办法]
matrix=new boolean [maxX][]; 
这里并没有对matrix初始化完全
只是初始化了
matrix[0]~matrix[maxX-1]=null

而在代码这里
matrix[x][y]=true; 
肯定报空指针异常了

修改为:
matrix=new boolean [maxX][maxY];

热点排行