I've been coding for about an hour and a half now, and for some reason I am getting this error. The full error is:
"Exception in thread "main" java.lang.NullPointerException
at HauntedHouse.livingRoomPath(HauntedHouse.java:98)
at TestHauntedHouse.main(TestHauntedHouse.java:8)
Java Result: 1"
After I run the the methods from my main class in my test class.
I am creating a Haunted House maze sort of game, and you select which room to go to, and which objects to interact with, if you interact with an object the game ends. The code is below, my guess is that I make an error somewhere in the if statement that involves the Chest, because every other path works, including the Bathroom path, and selecting objects in there. It's just the living room path that doesn't seem to work when you select the chest object, but I am not sure what the error is, I am not getting any error red marks in my main class.
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);
}
}
}