使用DOMParser和selectSingleNode的NullPointerException

时间:2011-01-07 14:43:19

标签: xpath dom4j

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)

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

1 个答案:

答案 0 :(得分:1)

我不知道问题的原因是什么,但我要做的第一件事就是一次走一条路,看看它无法返回你期望的地方。 / p>

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

等...