here i am just trying to parse an xml file and store the contents in an array lists, but everytime its giving java.Lang.NullPointerException at two points, pls help in debugging this…
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();
at this line it is giving a java null pointer exception
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){
at this lineits giving the same error
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);
}
}
}
here is the log cat for the same
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)
The problem is that
R.raw.options.xmlis not a valid URI so in this linedom=db.parse("R.raw.options.xml");, thedomvariable is null. You are trying to use methods of the null variabledoc(lineElement docEle = dom.getDocumentElement();inside theParseDocument(doc);method), that is why you get aNullPointerException. If you pass a valid URI indom=db.parse("valid_URI");method you will eliminate the twoNullPointerExceptions.EDIT Here is a complete example:
Hope that helps.