“线程中的异常”;“主要”;java.lang.NullPointerException错误
请帮帮我。我的代码中出现空指针异常错误。这是:“线程中的异常”;“主要”;java.lang.NullPointerException错误,java,nullpointerexception,twitter4j,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);
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); } }
它在前几行有“text”字段时运行,但当它遇到没有“text”字段的行时,会抛出null指针异常,而不是在控制台上打印“null”。救命啊 使用
==
操作符检查对象引用
if (text == null)
更改此项:
if(text.equals(null))
致:
如果“text”为null,text.equals将抛出NPE。使用==运算符。我发现可能有两个(可能有三个)地方可以获得NPE。你(特别是)用哪一行?谢谢你的快速回复。我刚刚做了,但仍然有同样的错误。你能给我们看一下stacktrace,以及你得到异常的确切行吗?stacktrace显示了什么?你能帮我们寄吗?
if(text == null)