I ran this program and it reported NullPointerException at this line
NodeList nodeList = root.getChildNodes();
Here is the whole thing:
package com.example.dom_technique;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class DOM extends Activity {
private Button button;
private TextView textView;
protected void onCreate(Bundle x) {
super.onCreate(x);
setContentView(R.layout.activity_dom);
button = (Button) findViewById(R.id.button1);
textView = (TextView) findViewById(R.id.textView1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
readXML();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void readXML() throws ParserConfigurationException, SAXException, IOException {
String output = "";
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputStream inputStream = getResources().openRawResource(R.drawable.student);
Document doc = builder.parse(inputStream);
Element root = doc.getDocumentElement();
NodeList nodeList = root.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
Element student = (Element) node;
String name = student.getAttribute("name");
String student_id = student.getAttribute("student_id");
NodeList subNodeList = student.getElementsByTagName("school");
String school = subNodeList.item(0).getTextContent();
subNodeList = student.getElementsByTagName("major");
String major = subNodeList.item(0).getTextContent();
output += name + " " + student_id + "\n" + school + "\n" + major +"\n" + " ***********" + "\n";
}
}
textView.setText(output);
}
}
And XML file I wrote by myself:
<?xml version="1.0" encoding="utf-8"?><students>
<student name="Henry Thompson" student_id="63827614">
<school>University of California, Berkeley</school>
<major>Computer Science</major>
</student>
<student name="Sheila Lopez" student_id="02817281">
<school>University of California, Los Angeles</school>
<major>Electrical Engineering</major>
</student>
<student name="Gabriel Carter" student_id="92717290">
<school>University of California, Davis</school>
<major>Mathematics</major>
</student>
<student name="Stephanie Nguyen" student_id="83729337">
<school>University of California, San Diego</school>
<major>Business Administraion, Concentration in Marketing</major>
</student>
Barely can I understand why, I thought I went through every requirements. I suspect the problem may come from the XML file. At first, I tried to create a XML file in drawable folder and it reported bug. Therefore I wrote in notepad, saved with xml extension and imported into drawable, then it was fine. Please help me!!! Thank you so much!!!!!!
Aucun commentaire:
Enregistrer un commentaire