I have following XML code
<?xml version="1.0" encoding="windows-1250"?>
<menu>
<item>
<id>0</id>
<name>Pizzerie u soudu</name>
<street>Havlíčkova 3</street>
<city>779 00 Olomouc</city>
<mobile>777035862</mobile>
<telephone>585223042</telephone>
<openinghours>10-22</openinghours>
</item>
<item>
<id>1</id>
<name>Pepinova pizza</name>
<street>©meralova 10</street>
<city>779 00 Olomouc</city>
<mobile>776102022</mobile>
<telephone>-</telephone>
<openinghours>8-22</openinghours>
</item>
</menu>
and I would like to parse it by following code:
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName("item");
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nl.item(i);
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_STREET, parser.getValue(e, KEY_STREET));
map.put(KEY_CITY, parser.getValue(e, KEY_CITY));
map.put(KEY_MOBILE, parser.getValue(e, KEY_MOBILE));
map.put(KEY_TELEPHONE, parser.getValue(e, KEY_TELEPHONE));
map.put(KEY_OPENINGHOURS, parser.getValue(e, KEY_OPENINGHOURS));
menuItems.add(map);
}
but I have problem on line
NodeList nl = doc.getElementsByTagName("item");
It returns java.lang.NullPointerException
What’s wrong?
It appears to me that you
XMLParsercode is your proprietary andgetDomElementmethod is returning NULL for some reason like file is not found in path or some other errors. Even if the standard API’s please check whether there are any condition that returns doc value as NULL.