繁体   English   中英

线程“ main”中的异常java.lang.NullPointerException与数组?

[英]Exception in thread “main” java.lang.NullPointerException with arrays?

我已经编码了大约一个半小时,由于某种原因,我遇到了这个错误。 完整的错误是:

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

在我的测试类中运行主类中的方法之后。

我正在创建“鬼屋”迷宫类游戏,如果与游戏结束的对象进行交互,则可以选择要去的房间以及要与之交互的对象。 代码在下面,我的猜测是我在涉及Chest的if语句中的某个地方犯了错误,因为其他所有路径都起作用,包括Bathroom路径,并在其中选择对象。 当您选择胸部对象时,只是客厅路径似乎不起作用,但是我不确定错误是什么,我的主类中没有任何错误红色标记。

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);

       }
   }
}

似乎这与以下事实有关:您实际上并未初始化String option4,而是对其进行了声明,然后,如果用户单击了bathroom或您使用option4 = options4 [choice4]对其进行了初始化。 然后在else中,您询问option4.equals(something)但是(因为我在if块内对其进行了初始化),所以当您调用方法“ equals”时,您引用的是空变量。 一种可能的解决方案是用“”初始化每个optionx变量,这样无论何时从它们调用方法时它们都不为null。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM