2013-06-15 2 views
0

nullPointerException이있는 것 같습니다. 내 코드를 검토하고 의견을 보내주십시오. 이것은 링크 된 목록에서 무작위로 레이블을 얻으려고 다른 클래스에서 호출하는 클래스 및 생성자입니다. 내가 그것을 호출 할 때 난 여전히 nullPointer 예외를 얻을 수 있지만 여기JLabel을 반환하는 동안 NullPointerException이 발생했습니다.

public class RandomHeuristic { 

    GameInterface game; 
    JLabel randomLabel; 
    public JLabel RandomHeuristic() { 
     randomLabel = (JLabel) game.labels.getFirst(); 
     int counter = 0; 
     do { 
      Collections.shuffle(game.labels); 
      randomLabel = (JLabel) game.labels.getFirst(); 
      counter++; 
      if (counter == 100) { 
       break; 
      } 
      /* 
      * Debugging 
      * System.out.println(randomLabel.getText()); 
      */ 
     } while (randomLabel != null && game.isLegalMove(randomLabel) == false); 
     //Retrieves and removes the head (first element) of this list. 
     if(randomLabel == null){ 
      RandomHeuristic(); 
     } 
     //game.labels.remove(randomLabel); 
     return randomLabel; 
    } 
} 

입니다 그리고 여기 내가 생성자를 호출하고 어디는 playHeuristicMove는(), 내가 제대로 작동 디버깅에 체크 JLabel를 기대하고있다 . RandomHeuristic randomHeuristicOne; playHeuristicMove(randomHeuristicOne.RandomHeuristic());

답변

2

이 아마도 당신이 그것을이 시작되지 않습니다 무슨 뜻 이죠

+1

의 인스턴스를 생성 한 적이있어 보인다 귀하의 gameGameInterface 객체를보고 싶을 : randomHeuristicOne이 같은 동일한 클래스에 만들어집니다? 나는 그것을 만들고 다음 반환 된 레이블 오전 그 통해 이루어집니다? 어쩌면 뭔가 다른 것을 의미할까요? – hedgehog

+1

@SkatZ GameInterface game .. 당신이 인스턴스를 만들지 않았기 때문에 필드가 null 인 것 같습니다. new 연산자는 인스턴스를 생성합니다. GameInterface game = new GameInterface(); (Java 인터페이스 인 경우 구현 자 클래스를 사용하십시오.) game.labels.getFirst() 호출을하기 전에 – Bartzilla

+1

시도했지만 아직 작동하지 않습니다! 왜 다른 어떤 아이디어가 null입니까? – hedgehog

관련 문제