在这里,我只是试图解析一个 xml 文件并将内容存储在一个数组列表中,但是每次它在两点给出 java.Lang.NullPointerException 时,请帮助调试这个......
public class XML_PARSER extends Activity {
String TAG= "XML_PARSER";
List optionList = new ArrayList();
Document dom;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parser);
Document doc=parseXmlFile();
在这一行,它给出了一个 java 空指针异常
ParseDocument(doc);
//printData();
}
private Document parseXmlFile(){
DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
dom=db.parse("R.raw.options.xml");
}
catch(ParserConfigurationException pce){
pce.printStackTrace();
}
catch(SAXException se){
se.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
return dom;
}
private void ParseDocument(Document dom){
在这个 lineits 给出了同样的错误
Element docEle = dom.getDocumentElement();
Node node;
NodeList n1= docEle.getElementsByTagName("Option");
if(n1!=null && n1.getLength()>0){
for(int i=0;i<n1.getLength();i++){
node=n1.item(i);
Element e1=(Element)n1.item(i);
Option e = getOption(e1,node);
optionList.add(e);
}
}
}
这是相同的日志猫
07-20 12:21:48.391: E/AndroidRuntime(836): Caused by: java.lang.NullPointerException
07-20 12:21:48.391: E/AndroidRuntime(836): at com.example.xml_parser.XML_PARSER.ParseDocument(XML_PARSER.java:67)
07-20 12:21:48.391: E/AndroidRuntime(836): at com.example.xml_parser.XML_PARSER.onCreate(XML_PARSER.java:38)
07-20 12:21:48.391: E/AndroidRuntime(836): at android.app.Activity.performCreate(Activity.java:5008)
07-20 12:21:48.391: E/AndroidRuntime(836): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-20 12:21:48.391: E/AndroidRuntime(836): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)