2014-09-19 6 views
0
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. 하지만 어떤 이유에서 나에게 널 포인터를주는 것

이것이 내 초자연식을 초기화하는 방법이다. 문제는 거기에있는 것일까?

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); 
    } 
} 
+2

어떻게'''crossecting()''을 호출합니까? –

+0

null 포인터 예외는 어디에서 발생합니까? – CMPS

+0

하나의 가능한 원인은 스레드와 주 프로그램이 개별적으로 실행되기 때문입니다. 그러나 다시, 문제를 이해하기위한 전체 (주석 처리 된) 코드를 보여주십시오. – Atieh

답변

0

입력 사항이 교차한다고 생각합니다. if 조건을 확인할 때 먼저 r1과 r2를 확인하십시오.

if(r1.isAlive==true && r2.isAlive==true && r1.rec.intersects(r2.rec)) { 
    ... 
} 
3

이와 같은 경우 stacktrace!

if(r1.rec.intersects(r2.rec) && r1.isAlive==true && r2.isAlive==true) 

(r1.recr2.rec가 null이 아닌 것으로 가정한다) 여기 r1.rec.intersects(r2.rec) 던져있어 정확하게하려면 널 포인터가 여기에서 발생되는 나는 그것에서 추측하고있다.

int rw = r.width; 

을 그리고 NPE가 발생하는 곳이다 :

당신은 AWT 패키지에 Rectangle#intersects(Rectangle r)의 코드를 선택하면 해당 라인 786에 당신이 볼 수 있습니다. 여기 rr2.rec이며 이는 null임을 나타냅니다. intersects()으로 전화하기 전에이 케이스를 확인하거나 intersecting(Sprite r1, Sprite r2)으로 전화하는 방법을 수정해야합니다.