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