我的代码按预期工作,当我输入此if
语句时出现问题:
if (sList.get(i).equals(null)){break;}
程序崩溃并显示以下调试信息 (NullPointerException):
在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 HangmanLexicon.(HangmanLexicon.java:51) 的 java.lang.NullPointerException (NativeConstructorAccessorImpl.java:39) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructorAccessorImpl.java:27) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:513) 在 java.lang.Class.newInstance0(Class.java:355) 在 java.lang.Class.newInstance(Class. java:308) 在 sun.applet.AppletPanel.createApplet(AppletPanel.java:807) 在 sun.applet.AppletPanel.runLoader(AppletPanel.java:714) 在 sun.applet.AppletPanel.run(AppletPanel.java:368) 在java.lang.Thread.run(Thread.java:680)
这是所有代码(是的,我知道它很乱,在学习新东西时看起来就是这样):
public HangmanLexicon()
{
/* 1. Open the data file HangmanLexicon.txt using a
* BufferedReader that will allow you to read it line by line. */
String readWord = "";
try
{
BufferedReader rd = new BufferedReader (new FileReader ("ShorterLexicon.txt"));
while (true)
{
readWord = rd.readLine();
println(readWord);
sList.add(readWord);
if (readWord == null){
println("Shit has hit the fan.");
break;}
}
rd.close();
}
catch (IOException ex)
{
println ("Shit has hit the fan.");
throw new ErrorException(ex);
}
println("readWord: "+ readWord);
/* 2. Read the lines from the file into an ArrayList. */
for (int i = 0; i < sList.size(); i++)
{
if (sList.get(i).equals (null)){println("done.");}
println("get: "+sList.get(i) + " pos: " + i);
}
}