NullPointerException using DOMParser and selectSingleNode
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 ?
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...
精彩评论