线程“main”java.lang.NullPointerException错误中的异常

时间:2014-03-22 17:20:55

标签: java nullpointerexception twitter4j

请帮帮我。我的代码中出现空指针异常错误。这是:

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

它运行的前几行有"文本"字段,但它遇到没有"文字"它抛出一个空指针异常而不是打印" null"在控制台上。求救!

3 个答案:

答案 0 :(得分:1)

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

if (text == null)

答案 1 :(得分:1)

改变这个:

if(text.equals(null))

为:

if(text == null)

答案 2 :(得分:0)

如果' text'为null,text.equals将抛出一个NPE。使用==运算符。

相关问题