0
我試圖解析一些XML,我得到一個NullPointerException
但我似乎無法找出我做錯了什麼:openRawResource NullPointerException異常
private Puzzle XMLfromFile(int xml) throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory spf= SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XMLHandler theHandler= new XMLHandler();
xr.setContentHandler(theHandler);
InputStream is = getResources().openRawResource(xml);
xr.parse(new InputSource(is)); //this crashes the program with the null pointer
return theHandler.currentPuzzle;
}
和來電:
try {
thePuzzle=XMLfromFile(R.raw.puzzle1);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
它實際上通過流.openRawResource(xml)應該打開一個原始資源作爲關聯資源的流數據。我試圖得到字符串,它使我無法將字符串轉換爲輸入流 – 2012-02-20 23:18:23
究竟是拋出NullPointerException。你可以發佈堆棧跟蹤嗎? – 2012-02-20 23:42:18