空指针异常检查

发布于 2025-01-04 19:05:14 字数 668 浏览 4 评论 0原文

我有一个类文件,当我编译时,该文件始终在特定行中显示空指针异常。我按如下方式处理空检查。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初心 2025-01-11 19:05:14

我无法通过上面发布的代码准确地判断您的代码的结构,但这应该修复 NullPointerException

Element div = doc.getElementById("eventTTL");
String attr = "";

if(div != null)
    attr = div.attr("eventTTL");

如果您在那之后继续收到异常,我猜问题出在代码的其他地方,并且你需要更新你的例子。

I can't tell by the code posted above exactly how your code is structured, but this should fix the NullPointerException:

Element div = doc.getElementById("eventTTL");
String attr = "";

if(div != null)
    attr = div.attr("eventTTL");

If you continue getting Exceptions after that, I would guess the issue is elsewhere in the code and you need to update your example.

北斗星光 2025-01-11 19:05:14

您所做的代码更改将导致编译错误。不会创建任何类文件,因此您可能正在运行旧的损坏的类文件。

您可能想要这样做(扩展 if 的范围)。

doc = Jsoup.parse(html, brandUrl);

Element div = doc.getElementById("eventTTL");
if(div != null) {
    String attr = div.attr("eventTTL");
    Date closingDate = new Date(Long.parseLong(attr));
    Elements mainForm = doc.select("div#main-form");
    Elements mainDivs = mainForm.select("DIV");
    ...
}

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).

doc = Jsoup.parse(html, brandUrl);

Element div = doc.getElementById("eventTTL");
if(div != null) {
    String attr = div.attr("eventTTL");
    Date closingDate = new Date(Long.parseLong(attr));
    Elements mainForm = doc.select("div#main-form");
    Elements mainDivs = mainForm.select("DIV");
    ...
}
孤独陪着我 2025-01-11 19:05:14

如果没有看到你的堆栈跟踪,我只是猜测,但是:

从你所说的错误行来看,这意味着 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 your doc has no Element with an Id of eventTTL.

Try ensuring that your doc is valid, and the it actually has a eventTTL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文