2014-11-01 4 views
0

저는 약 한 시간 반 동안 코딩을 해왔고 어떤 이유로이 오류가 발생합니다. 전체 오류 :스레드 "main"의 예외 java.lang.NullPointerException (배열 포함)?

"Exception in thread "main" java.lang.NullPointerException 
    at HauntedHouse.livingRoomPath(HauntedHouse.java:98) 
    at TestHauntedHouse.main(TestHauntedHouse.java:8) 
Java Result: 1" 

내 테스트 클래스에서 내 주 클래스의 메서드를 실행 한 후.

귀한 집 미로 게임을 만들고 게임을 끝내는 대상과 상호 작용할 경우 어떤 방을 선택하고 어떤 대상과 상호 작용할 것인지를 선택합니다. 아래 코드는 내 생각 엔 가슴 경로가 포함 된 if 문에서 어딘가에 오류가 발생한다는 것입니다. 다른 모든 경로는 욕실 경로를 포함하여 작동하고 거기에서 객체를 선택하기 때문입니다. 가슴 객체를 선택할 때 작동하지 않는 거실 경로 일 뿐이지 만 오류가 무엇인지 확신 할 수 없으며 기본 클래스에서 오류가 발생하지 않습니다. 당신이 실제로 문자열 | 옵를 초기화하지 않은 사실과 관련이있다처럼

import javax.swing.JOptionPane; 
import javax.swing.ImageIcon; 

public class HauntedHouse { 

    final ImageIcon map1 = new ImageIcon("C:\\Users\\Damian\\Pictures\\Designs\\Halloween Contest Card.jpg"); 
    private String playerName, option1, option2, option3, option4, option5; 
    private int result1; 

    public void askName() 
    { 
     playerName = JOptionPane.showInputDialog(null, "Welcome to Damian's Spooky Haunted House of  Harrowing. Please enter your name below to enter if you dare...", 
        "Welcoe to the Haunted House", JOptionPane.INFORMATION_MESSAGE); 
    } 

    public void showMap() 
    { 
     JOptionPane.showMessageDialog(null, "Are you ready to enter " + playerName + "? If not too bad, because here's where you start.", 
            "Your Map", JOptionPane.INFORMATION_MESSAGE); 
     JOptionPane.showMessageDialog(null, "The red dot is you.", 
            "Your Map", JOptionPane.INFORMATION_MESSAGE, map1); 
    } 

    public void livingRoomPath() 
    { 
     if (result1 == JOptionPane.YES_OPTION) 
     { 
     String [] options1 = {"Living Room", "Dinning Room", "Upstairs"}; 

     int choice1 = JOptionPane.showOptionDialog(
         null, 
         "Where would you like to move to next?", 
         "Option", 
         JOptionPane.YES_NO_CANCEL_OPTION, 
         JOptionPane.QUESTION_MESSAGE, 
         null, 
         options1, 
         options1[2]); 

      option1 = options1[choice1]; 
     } 

     if(option1.equals("Living Room")) 
     { 
      String [] options2 = {"Interact with an object", "Continue to Another Room"}; 

      int choice2 = JOptionPane.showOptionDialog(
         null, 
         "Welcome to the Living Room. The Master lives here. What would you like to do next?", 
         "Option", 
         JOptionPane.YES_NO_CANCEL_OPTION, 
         JOptionPane.QUESTION_MESSAGE, 
         null, 
         options2, 
         options2[1]); 

      option2 = options2[choice2]; 

      if(option2.equals("Interact with an object")) 
      { 
       String [] options3 = {"Chest"}; 

       int choice3 = JOptionPane.showOptionDialog(
          null, 
          "Which object would you like to interact with?", 
          "Option", 
          JOptionPane.YES_NO_CANCEL_OPTION, 
          JOptionPane.QUESTION_MESSAGE, 
          null, 
          options3, 
          options3[0]); 

          option3 = options3[choice3]; 

       JOptionPane.showMessageDialog(null, "Ghost escapes and scares you to death.", 
              "Game Over", JOptionPane.INFORMATION_MESSAGE); 
       JOptionPane.showMessageDialog(null, "You've been scared. Thanks for playing!", 
              "Game Over", JOptionPane.INFORMATION_MESSAGE); 
      } 

      else if(option2.equals("Continue to Another Room")) 
      { 
       String [] options4 = { "Bathroom",}; 

       int choice4 = JOptionPane.showOptionDialog(
          null, 
          "Where would you like to move to next?", 
          "Option", 
          JOptionPane.YES_NO_CANCEL_OPTION, 
          JOptionPane.QUESTION_MESSAGE, 
          null, 
          options4, 
          options4[0]); 

       option4 = options4[choice4]; 
      } 

      if(option4.equals("Bathroom")) 
      { 
       String [] options5 = { "Shower", "Mirror"}; 

       int choice5 = JOptionPane.showOptionDialog(
          null, 
          "Which object would you like to interact with?", 
          "Option", 
          JOptionPane.YES_NO_CANCEL_OPTION, 
          JOptionPane.QUESTION_MESSAGE, 
          null, 
          options5, 
          options5[1]); 

       option5 = options5[choice5]; 
      } 

      if(option5.equals("Shower")) 
      { 
       JOptionPane.showMessageDialog(null, "Room suddenly steams up and you feel fingers touching the back of your neck", 
              "Game Over", JOptionPane.INFORMATION_MESSAGE); 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null, "See a bloody face looking back at you.", 
              "Game Over", JOptionPane.INFORMATION_MESSAGE); 
      } 
       JOptionPane.showMessageDialog(null, "You've been scared. Thanks for playing!", 
              "Game Over", JOptionPane.INFORMATION_MESSAGE); 

     } 
    } 
} 
+2

[Null 포인터 예외 란 무엇이며 어떻게 수정합니까?] (http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) –

+0

복제본인지 확실하지 않은 경우, 이것에 관련된 다른 질문이지만, 반복되는 코드에 여러 가지가 있기 때문에 이상합니다. 그리고 모든 코드가 작동하고 코드의 한 부분 만 작동하지 않습니다. – beginnercoder010812

+0

이것은 괜찮습니까 -'String [] options4 = { "욕실",};'추가 쉼표? – Leron

답변

0

것 같다, 당신은 그것을 선언 한 다음 사용자 욕실 클릭하거나 뭔가 당신이 | 옵 = options4으로 초기화하면 [choice4] . 그리고 else에서 option4.equals (something)를 물어보십시오.하지만 if 블록 내에서 초기화 한 이후로 ("추측하고 있습니다") 메소드를 "equals"라고 부르면 null 변수를 참조하게됩니다. 가능한 해결책은 각 optionx 변수를 ""로 초기화하여 메서드를 호출 할 때마다 null이 아니므로

+0

고맙습니다. 코드의 시작 부분에서 ""와 같게 설정하면 문제가 해결되는 것 같습니다. 나는 결국 코드를 ​​변경해야하고 else 문을 if 문으로 변환해야합니다. 왜냐하면 가슴 경로를 선택했을 때 "유령이 당신을 놀라게 할뿐 아니라" "피 묻은 얼굴"이라는 메시지가 나왔기 때문입니다. 미러 텍스트도 마찬가지입니다. 하지만 지금은 잘 작동합니다. – beginnercoder010812

관련 문제