【发布时间】:2014-09-19 01:32:11
【问题描述】:
Exception in thread "Thread-1" java.lang.NullPointerException
at java.awt.Rectangle.intersects(Rectangle.java:786)
at Robotron.intersecting(Robotron.java:182)
at Robotron.run(Robotron.java:349)
at java.lang.Thread.run(Thread.java:745)
麻烦就在这里:
public void intersecting(Sprite r1, Sprite r2)
{
System.out.println("The grunts isAlive is: "+r1.isAlive+" his xpos is: "+r1.rec.x+" his ypos is: "+r1.rec.y);
if(r1.rec.intersects(r2.rec) && r1.isAlive==true && r2.isAlive==true)
{
r1.isAlive=false;
r2.isAlive=false;
}
}
我的 System.out 的输出是:The grunts isAlive is: true his xpos is: 936 his ypos is: 478。但由于某种原因,它给了我一个空指针
这就是我初始化 Grunts 的方式,也许问题出在那儿?
for(int i=0; i<grunt.length;i++)
{
int randX = (int)(Math.random()*worldx);
int randY = (int)(Math.random()*worldy);
if(hero.outerCircle.inCircle(randX,randY)!=true)
{
grunt[i] = new EnemyD4(randX,randY, worldx, worldy, 50,70);
}
if(hero.outerCircle.inCircle(randX,randY)==true)
{
randX+=100;
grunt[i] = new EnemyD4(randX,randY, worldx, worldy, 50,70);
}
}
【问题讨论】:
-
你怎么打电话给
intersecting()? -
空指针异常发生在哪里?
-
一个可能的原因是线程和主程序是分开执行的。但是,再次向我们展示完整(注释)代码以了解问题。
标签: java arrays nullpointerexception rectangles intersect