使用 DOMParser 和 selectSingleNode 的 NullPointerException

发布于 2024-10-11 04:58:33 字数 710 浏览 0 评论 0原文

String url = "http://www.amazon.com/Classic-Starts-Great-Expectations/dp/1402766459/ref=sr_1_1?s=books&ie=UTF8&qid=1294405505&sr=1-1";

DOMParser parser = new DOMParser();
parser.parse(url);

Document document = parser.getDocument();
DOMReader reader = new DOMReader();
org.dom4j.Document nhddoc = reader.read(document);

//book price
Node price = nhddoc.selectSingleNode("/HTML/BODY/DIV[2]/FORM/TABLE[3]/TBODY/TR/TD/DIV/TABLE/TBODY/TR/TD[2]/B");
System.out.println(price.getText().toString().trim());

我得到的错误是:

Exception in thread "main" java.lang.NullPointerException
        at nekodom4j.Main.main(Main.java:44)

空指针异常是否意味着没有选择节点?

String url = "http://www.amazon.com/Classic-Starts-Great-Expectations/dp/1402766459/ref=sr_1_1?s=books&ie=UTF8&qid=1294405505&sr=1-1";

DOMParser parser = new DOMParser();
parser.parse(url);

Document document = parser.getDocument();
DOMReader reader = new DOMReader();
org.dom4j.Document nhddoc = reader.read(document);

//book price
Node price = nhddoc.selectSingleNode("/HTML/BODY/DIV[2]/FORM/TABLE[3]/TBODY/TR/TD/DIV/TABLE/TBODY/TR/TD[2]/B");
System.out.println(price.getText().toString().trim());

the error i get is :

Exception in thread "main" java.lang.NullPointerException
        at nekodom4j.Main.main(Main.java:44)

does the null pointer exception means no node was selected ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

有深☉意 2024-10-18 04:58:33

我不太清楚问题的原因可能是什么,但调试它时我要做的第一件事就是一次走一层路径,看看它在哪里无法返回您期望的结果。

Node html = nhddoc.selectSingleNode("/HTML");
/* maybe print out some information about the Node just acquired */
Node body = html.selectSingleNode("/BODY");

ETC...

I have no particular idea what the cause of the problem might be, but the first thing I would do to debug it would be to walk the path one level at a time and see where it fails to return what you expect.

Node html = nhddoc.selectSingleNode("/HTML");
/* maybe print out some information about the Node just acquired */
Node body = html.selectSingleNode("/BODY");

etc...

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