저는 약 한 시간 반 동안 코딩을 해왔고 어떤 이유로이 오류가 발생합니다. 전체 오류 :스레드 "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);
}
}
}
[Null 포인터 예외 란 무엇이며 어떻게 수정합니까?] (http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) –
복제본인지 확실하지 않은 경우, 이것에 관련된 다른 질문이지만, 반복되는 코드에 여러 가지가 있기 때문에 이상합니다. 그리고 모든 코드가 작동하고 코드의 한 부분 만 작동하지 않습니다. – beginnercoder010812
이것은 괜찮습니까 -'String [] options4 = { "욕실",};'추가 쉼표? – Leron