线程" main"中的例外情况带数组的java.lang.NullPointerException?

时间:2014-11-01 20:14:49

标签: java 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);

       }
   }
}

1 个答案:

答案 0 :(得分:0)

这似乎与你没有实际初始化String option4,你声明它然后如果用户点击了浴室或者你用option4 = options4 [choice4]初始化它的事实有关。然后在其他地方,你问是否select4.equals(某事)但是(我猜测)因为你在if块中初始化它,当你调用方法" equals"你引用了一个空变量。一个可能的解决方案是使用""初始化每个optionx变量,这样当你从它们调用方法时它们就不会为空