Java XML 读取器错误

发布于 2024-11-19 08:59:00 字数 2040 浏览 3 评论 0原文

当我运行 XML 阅读器的开头时,出现错误:

public static void main(String[] args) 
{
    System.out.println("XML Reader");

    try
    {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse("database.xml");

        //normalize text
        doc.getDocumentElement().normalize();
        System.out.println("The root of this doc is "+doc.getDocumentElement().getNodeName());

        NodeList listOfAddresses = doc.getElementsByTagName("address");
        int totalAddresses = listOfAddresses.getLength();
        System.out.println(totalAddresses+" addresses in "+ doc.getDocumentElement().getNodeName());

        //main loop
        for(int i = 0; i<listOfAddresses.getLength(); i++)
        {
            Node items = listOfAddresses.item(i);

            if(items.getNodeType() == Node.ELEMENT_NODE)
            {
                System.out.println("Address #"+i);

                Element element = (Element)items;

                NodeList nameList = element.getElementsByTagName("name");
                Element nameElement = (Element)nameList.item(0);
                NodeList nameOutput = nameElement.getChildNodes();


                System.out.println("name: "+nameElement);
            }
        }


    }

    catch(SAXParseException err)
    {
        System.out.println("Sax Parse Exception error on line "+err.getLineNumber());
    }

    catch(SAXException e)
    {
        System.out.println("SAX Exception error");
        Exception x = e.getException();
        ((x == null) ? e : x).printStackTrace();
    }

    catch(Throwable t)
    {
        System.out.println("Trowable error");
        t.printStackTrace();
    }
}

netbeans 给出以下输出:

run:
XML Reader
The root of this doc is database
2 addresses in database
java.lang.NullPointerException
Address #0
Trowable error
    at xmlreader.XMLreader.main(XMLreader.java:42)

有人能帮我解决这个问题吗?

I get an error when i run the beginning of my XML reader:

public static void main(String[] args) 
{
    System.out.println("XML Reader");

    try
    {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse("database.xml");

        //normalize text
        doc.getDocumentElement().normalize();
        System.out.println("The root of this doc is "+doc.getDocumentElement().getNodeName());

        NodeList listOfAddresses = doc.getElementsByTagName("address");
        int totalAddresses = listOfAddresses.getLength();
        System.out.println(totalAddresses+" addresses in "+ doc.getDocumentElement().getNodeName());

        //main loop
        for(int i = 0; i<listOfAddresses.getLength(); i++)
        {
            Node items = listOfAddresses.item(i);

            if(items.getNodeType() == Node.ELEMENT_NODE)
            {
                System.out.println("Address #"+i);

                Element element = (Element)items;

                NodeList nameList = element.getElementsByTagName("name");
                Element nameElement = (Element)nameList.item(0);
                NodeList nameOutput = nameElement.getChildNodes();


                System.out.println("name: "+nameElement);
            }
        }


    }

    catch(SAXParseException err)
    {
        System.out.println("Sax Parse Exception error on line "+err.getLineNumber());
    }

    catch(SAXException e)
    {
        System.out.println("SAX Exception error");
        Exception x = e.getException();
        ((x == null) ? e : x).printStackTrace();
    }

    catch(Throwable t)
    {
        System.out.println("Trowable error");
        t.printStackTrace();
    }
}

netbeans is giving me the following output:

run:
XML Reader
The root of this doc is database
2 addresses in database
java.lang.NullPointerException
Address #0
Trowable error
    at xmlreader.XMLreader.main(XMLreader.java:42)

Could someone help me figure this one out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的快感 2024-11-26 08:59:00

您没有检查是否存在元素 0。本质上,如果索引无效,.item() 将返回 null,因此可能不存在索引为 0 的项目返回 null,然后您尝试调用 getChildNodes () 位于空指针上。因此你的 NPE。

您应该迭代节点列表中的元素。

更改

Element nameElement = (Element)nameList.item(0);

for(int x = 0; x < nameList.getLength(); x++) {
  nameElement = nameList.item(x);
  NodeList nameOutput = nameElement.getChildNodes();
  System.out.println("name: "+nameElement);
}

You're not checking if there IS an element 0. Essentially, .item() will return null if the index isn't valid, as such there may not be an item with index 0 which returns null and then you try to call getChildNodes() on the null pointer. Hence your NPE.

You should be iterating over the elements in the node list.

change

Element nameElement = (Element)nameList.item(0);

to

for(int x = 0; x < nameList.getLength(); x++) {
  nameElement = nameList.item(x);
  NodeList nameOutput = nameElement.getChildNodes();
  System.out.println("name: "+nameElement);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文