使用 DOMParser 和 selectSingleNode 的 NullPointerException
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太清楚问题的原因可能是什么,但调试它时我要做的第一件事就是一次走一层路径,看看它在哪里无法返回您期望的结果。
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.
etc...