openRawResource NullPointerException

时间:2012-02-20 00:57:16

标签: android xml 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();
    }

2 个答案:

答案 0 :(得分:0)

您正在将整数传递到InputSource()。您必须将整个XML文件作为字符串(getString(int id))或其他一些检索文件的方法获取。

答案 1 :(得分:0)

抱歉,我需要经常睡觉我的问题,我发现我删除的代码片段导致我的处理程序抛出空指针问题。谢谢你,再次抱歉。