我让用户在 Applet 中输入 TextField,我想检查他们输入的字符串是否等于答案 ArrayList 中的相应元素,但是我得到一个很大的编译时错误:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at com.game.main.AppletMain.actionPerformed(AppletMain.java:72) // etc
这是代码,主类(我已经编辑了不重要的部分:
private TextField commandInput;
private TextArea messageDisplay;
private Message messages;
private Answer answers;
private int currentLevel;
@Override
public void actionPerformed(ActionEvent e) {
String userInput = commandInput.getText();
if (userInput == answers.getIntroAnswers().get(0)) {
messageDisplay.append("\n \n" + userInput + "\n \n");
commandInput.selectAll();
}
getCurrentLevel();
}
这是包含 ArrayList 的 Answers 类:
package com.game.main;
import java.util.ArrayList;
public class Answer {
private ArrayList<String> introAnswers;
private String introAns1 = "go", introAns2 = "Go";
public Answer() {
introAnswers = new ArrayList<String>();
introAnswers.add(introAns1);
introAnswers.add(introAns2);
}
public ArrayList<String> getIntroAnswers() {
return introAnswers;
}
}