简体   繁体   中英

Exception in thread "main" java.lang.NullPointerException error

please help me. I'm getting a null pointer exception error in my code. Here it is:

    File strfile = new File("15.json");
    Scanner scan = new Scanner (strfile);

    while(scan.hasNextLine())
    {
        String line = scan.nextLine();
        Status statjson = DataObjectFactory.createStatus(json);
        String text = statjson.getText();

    if (text == null)
    {
        System.out.print("null");
    }
           else { System.out.print(text); }     }

it runs for the first few lines that have a "text" field but the moment it comes across a line without "text" it throws a null pointer exception instead of printing "null" on the console. Help please!

使用==运算符检查对象引用

if (text == null)

Change this:

if(text.equals(null))

to:

if(text == null)

If 'text' is null, text.equals will throw a NPE. Use the == operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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