简体   繁体   English

Java:BufferedReader和NullPointerException

[英]Java: BufferedReader and NullPointerException

My code works as supposed, the a problem arises when I type in this if statement: 我的代码按预期工作,当我在此if语句中键入时会出现问题:

if (sList.get(i).equals(null)){break;}

the program crashes with the following debug information (NullPointerException): 程序崩溃,并显示以下调试信息(NullPointerException):

java.lang.NullPointerException at HangmanLexicon.(HangmanLexicon.java:51) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at java.lang.Class.newInstance0(Class.java:355) at java.lang.Class.newInstance(Class.java:308) at sun.applet.AppletPanel.createApplet(AppletPanel.java:807) at sun.applet.AppletPanel.runLoader(AppletPanel.java:714) at sun.applet.AppletPanel.run(AppletPanel.java:368) at java.lang.Thread.run(Thread.java:680) HangmanLexicon上的java.lang.NullPointerException(HangmanLexicon.java:51)在sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native方法)在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)在sun.reflect.DelegatingConstructorAccess (DelegatingConstructorAccessorImpl.java:27)在java.lang.Class.newInstance0(Class.java:355)在java.lang.Class.newInstance(Class.java.lang.reflect.Constructor.newInstance(Constructor.java:513) 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)

Here is all the code (yeah I know it´s messy, it looks that way when learning new stuff): 这是所有代码(是的,我知道这很杂乱,学习新东西时看起来像这样):

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

}

Don't test for null this way, instead use: 不要以这种方式测试null ,而是使用:

if (sList.get(i) == null)

Your usage of equals has a problem in that a NullPointerException will naturally occur if the result of the get is null : because you can't call a method on null . 使用equals存在一个问题,如果get的结果为null ,则自然会发生NullPointerException :因为您不能在null上调用方法。

It seems sList.get(i) is null and you are calling equals() operation on null which results in NullPointerException . 似乎sList.get(i)null并且您正在对null调用equals()操作,这将导致NullPointerException

Use 采用

if (sList.get(i)== null)

instead of 代替

if (sList.get(i).equals (null))

when you have a null value in your array, you can not call equal function, simply compare with null 当数组中null值时,就不能调用equal函数,只需与null比较即可

if(sList.get(i) == null){
    println("done.");
}else{
    println("get: "+sList.get(i) + " pos: " + i);
}

i suggesting you to use function ready of class BufferedReader in while loop 我建议您在while循环中使用BufferedReader类的函数ready

while(rd.ready()){
   // read 
} 

see ready() 看到ready()

The bufferedReader store the value of String as "null" and not null if br.readLine() is empty bufferedReader将String的值存储为“ null”,如果br.readLine()为空,则不为null

So try checking 所以尝试检查

(!Stringval.equalisIgnoreCase("null"))

instead of 代替

(Stringval != null) or (Stringval.equalsIgnoreCase(null))

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

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