空指针异常检查
我有一个类文件,当我编译时,该文件始终在特定行中显示空指针异常。我按如下方式处理空检查。
108 doc = Jsoup.parse(html, brandUrl);
109 Element div = doc.getElementById("eventTTL");
110 String attr = div.attr("eventTTL");
111 Date closingDate = new Date(Long.parseLong(attr));
112 Elements mainForm = doc.select("div#main-form");
113 Elements mainDivs = mainForm.select("DIV");
java.lang.NullPointerException 在 com.textT.at.Chickyur.main(Chickyur.java:110)
if(div != null)
String attr = div.attr("eventTTL");
Also tried
if(div.attr("eventTTL") != null)
String attr = div.attr("eventTTL");
我仍然不断收到异常。怎么了?有什么想法吗?
I have a class file which when i compile shows me Null pointer exception in a particular line all the time. I handled null check as below.
108 doc = Jsoup.parse(html, brandUrl);
109 Element div = doc.getElementById("eventTTL");
110 String attr = div.attr("eventTTL");
111 Date closingDate = new Date(Long.parseLong(attr));
112 Elements mainForm = doc.select("div#main-form");
113 Elements mainDivs = mainForm.select("DIV");
java.lang.NullPointerException
at com.textT.at.Chickyur.main(Chickyur.java:110)
if(div != null)
String attr = div.attr("eventTTL");
Also tried
if(div.attr("eventTTL") != null)
String attr = div.attr("eventTTL");
Still i keep getting the exceptions. What is wrong? any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我无法通过上面发布的代码准确地判断您的代码的结构,但这应该修复
NullPointerException
:如果您在那之后继续收到异常,我猜问题出在代码的其他地方,并且你需要更新你的例子。
I can't tell by the code posted above exactly how your code is structured, but this should fix the
NullPointerException
:If you continue getting Exceptions after that, I would guess the issue is elsewhere in the code and you need to update your example.
您所做的代码更改将导致编译错误。不会创建任何类文件,因此您可能正在运行旧的损坏的类文件。
您可能想要这样做(扩展 if 的范围)。
The code change you made will result in a compile error. No class file will be created, so you are probably running the old broken class file.
You probably want do do this (extend the scope of the if).
如果没有看到你的堆栈跟踪,我只是猜测,但是:
从你所说的错误行来看,这意味着
div
为空,这表明上一行是罪魁祸首,这将意味着您的doc
没有 ID 为eventTTL
的元素。尝试确保您的
doc
有效,并且它实际上具有eventTTL
。Without seeing your stack trace, I'm only guessing, but:
From the line you say is the error, it means that
div
is null, which would indicate that the previous line is the culprit, which would mean that yourdoc
has no Element with an Id ofeventTTL
.Try ensuring that your
doc
is valid, and the it actually has aeventTTL
.