2015-01-28 6 views
0

프로그래밍 수업 소개를위한 프로젝트에서 작업 중이며 약간의 문제가 발생했습니다. 우리는 사이드 스크롤러를 만들고 있는데, 지금 스코어 카운터에서 일하고 있습니다. 내 문제는 (모든 프레임 한 번 호출됩니다) act 메서드 이외의 다른 카운터 클래스에 대한 참조를 만들려고 할 때 null 포인터 예외 오류가 발생합니다. 내 코드가 들어있는 zip 파일을 here에 다운로드 할 수 있습니다.nullPointerException 오류 greenfoot

편집 :

public class HeroMissile extends Missiles 

{

/** 
* Act - do whatever the HeroMissile wants to do. This method is called whenever 
* the 'Act' or 'Run' button gets pressed in the environment. 
*/ 
public void act() 
{ 
    move(8); 
    remove(); 
}  

public void remove() { 
    if(isTouching(Drone.class)) { 
     removeTouching(Drone.class); 
     getWorld().addObject(new Explosion(), getX(), getY()); 
     getWorld().removeObject(this); 
     addScore(); 
     return; 
    } 
} 

public void addScore() { 
    City cityWorld = (City) getWorld(); 
    **Counter scoreCounter = cityWorld.getCounter();** 
    scoreCounter.add(1); 
} 

}

답변

0

당신이 세상에서 자신을 제거 후에는 [addScore()에서] getWorld()를 호출 : 다음은 잘못된 코드입니다. 이 경우 getWorld()은 null을 반환하므로 널 포인터 예외가 발생합니다. 전 세계에서 자신을 삭제하기 전에 점수를 추가하려면 remove()의 순서를 변경하십시오.