openRawResource NullPointerException
我正在尝试解析一些 xml,但收到 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();
}
I'm trying to parse some xml and i'm getting a NullPointerException
but i can't seem to figure out what i did wrong:
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;
}
and the caller:
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在将一个整数传递到
InputSource()
中。您必须以字符串形式获取整个 XML 文件 (getString(int id)
),或者使用其他检索文件的方法。You are passing an integer into the
InputSource()
. You would have to get the entire XML file as string (getString(int id)
), or some other method of retrieving the file.抱歉,我需要更频繁地思考我的问题,我发现我删除的代码片段导致我的处理程序抛出空指针问题。谢谢你,再次抱歉。
Sorry i need to sleep over my problems a little more often, i found out that a code snipit i deleted cause my handler to throw the null pointer problem. Thanks you, and again sorry.