2012-02-20 171 views
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(); 
    } 

回答

0

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

0

您正在将一个整数传入InputSource()。您将不得不将整个XML文件作为字符串(getString(int id))或其他一些检索文件的方法。

+0

它实际上通过流.openRawResource(xml)应该打开一个原始资源作为关联资源的流数据。我试图得到字符串,它使我无法将字符串转换为输入流 – 2012-02-20 23:18:23

+0

究竟是抛出NullPointerException。你可以发布堆栈跟踪吗? – 2012-02-20 23:42:18