java帮忙看看代码那里错了

来源:百度知道 编辑:UC知道 时间:2024/05/17 04:59:23
import java.io.File;
import java.io.IOException;

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;

public class DOMStudentsInfo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db=dbf.newDocumentBuilder();
Document d=db.parse(new File("students.xml"));
NodeList nl=d.getElementsByTagName("student");
int len=nl.getLength();
for(int i=0;i<len;i++)
{
Element e=(Element)nl.item(i);

Node n=e.getElementsByT

String strName=n.getFirstChild().getNodeValue(); 这句话报了空指针异常,我猜是不是Node n没有获取到相应的值,故当n在获取getFirstChild().getNodeValue()的时候就报错了

祝楼主早日成功!

该类36行,你引用了一个空对象

Node n=e.getElementsByTagName("mame").item(0);

应该是"name"