openRawResource NullPointerException

发布于 2025-01-07 03:29:04 字数 1083 浏览 3 评论 0原文

我正在尝试解析一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云巢 2025-01-14 03:29:04

您正在将一个整数传递到 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.

墨离汐 2025-01-14 03:29:04

抱歉,我需要更频繁地思考我的问题,我发现我删除的代码片段导致我的处理程序抛出空指针问题。谢谢你,再次抱歉。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文