java NullPointerException when parsing XML
Posted
by behrk2
on Stack Overflow
See other posts from Stack Overflow
or by behrk2
Published on 2010-03-08T01:48:08Z
Indexed on
2010/03/08
1:49 UTC
Read the original article
Hit count: 377
Hi Everyone,
I keep receiving a java.lang.NullPointerException while trying to parse out the values of ths tags in the following XML sample:
<?xml version="1.0" standalone="yes"?>
<autocomplete>
<autocomplete_item>
<title short="Forrest Gump"></title>
</autocomplete_item>
<autocomplete_item>
<title short="Forrest Landis"></title>
</autocomplete_item>
<autocomplete_item>
<title short="Finding Forrester"></title>
</autocomplete_item>
<autocomplete_item>
<title short="Menotti: The Medium: Maureen Forrester"></title>
</autocomplete_item>
</autocomplete>
Here is my parsing code, can anyone see where I am going wrong? Thanks!
public String parse(String element) {
Document doc = null;
String result = null;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = null;
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
docBuilder.isValidating();
try {
doc = docBuilder.parse(input);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
doc.getDocumentElement().normalize();
NodeList list = doc.getElementsByTagName(element);
_node = new String();
_element = new String();
for (int i = 0; i < list.getLength(); i++) {
Node value = list.item(i).getChildNodes().item(0);
_node = list.item(i).getNodeName();
_element = value.getNodeValue();
result = _element;
SearchResults searchResults = new SearchResults();
searchResults.setTitles(result);
Vector test = searchResults.getTitles();
for (int p = 0; p < test.size(); p++) {
System.out.println("STUFF: " + test.elementAt(p));
}
}// end for
return result;
}
© Stack Overflow or respective owner