我有以下XML代碼解析XML返回NullPointerException異常
<?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>
,我想通過以下代碼來分析它:
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);
}
,但我對線路問題
NodeList nl = doc.getElementsByTagName("item");
它返回java.lang.NullPointerException
有什麼問題?
doc is null so NPE –
如何初始化doc?你能否附上該代碼?像Samir說的 - doc是空的。 – Gambrinus
請提供初始化「doc」變量的代碼。 –