Pregunta

I am trying to learn a bit about passing data between applications using XML. I wrote a sub in Excel that generates an order from Spreadsheet data, and then writes the order to an XML file (XML sample at end of question). I am now trying to write a function in Java to iterate through the file and print out the values. To do this, I test to see if a given node .hasChildNodes(), and, if it does, I try to iterate through the child nodes too. But the test doesn't seem to work as I thought it would, and the output looks like the program is going ahead and attempting to print the names of child nodes that don't exist. At the end, it throws a NullPointerException, because it tries to reference the child of a node that doesn't exist. I'm clearly making a mistake somewhere, and would appreciate any help finding out where!

Java project:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class rXML {
    public static void main(String[] args) {
        try{
            File file = new File("C:\\Users\\T\\Documents\\order.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);

            Element n;
            Node n2;

            doc.getDocumentElement().normalize();

            n = doc.getDocumentElement();
            System.out.println("Root: " + n.getNodeName());

            for (int i = 0; i < n.getChildNodes().getLength(); i++){
                n2 = n.getChildNodes().item(i);
                System.out.println("Sibling: " + n2.getNodeName());
                if (n2.hasChildNodes()){
                    for (int j = 0; j < n2.getChildNodes().getLength(); j++){
                        System.out.println("Child Of Sibling: " + n2.getChildNodes().item(j).getNodeName());
                        if (n2.getChildNodes().item(j).hasChildNodes()){
                            for (int h = 0; h < n2.getChildNodes().getLength(); h++){
                                System.out.println("Child Of Child of Sibling: " + n2.getChildNodes().item(j).getChildNodes().item(h).getNodeName());//This is line 37
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

XML file (contains randomly generated data):

<?xml version="1.0" encoding="UTF-8"?>

-<Order>

<OrdNum>1000124</OrdNum>

<Date>05/04/2014</Date>


-<Customer>

<FName>debsnofmttkxtcqsalkhzuskthjlyd</FName>

<SName>qjceclyomzfjkhndnzoxrlsbtsmefi</SName>

<Email>fqmrxjhhdnfpjwmertpuevfybbujmd@hotmail.com</Email>

</Customer>

<TotalValue>6312.5434</TotalValue>

<ValPlusVAT>7139.92614</ValPlusVAT>


-<Items>


-<Item1>

<Code>8060397</Code>

<Desc>someDes</Desc>

<Price>145.2365</Price>

<VATExempt>True</VATExempt>

</Item1>


-<Item2>

<Code>7813265</Code>

<Desc>someDes</Desc>

<Price>820.6028</Price>

<VATExempt>False</VATExempt>

</Item2>


-<Item3>

<Code>9754561</Code>

<Desc>someDes</Desc>

<Price>823.8404</Price>

<VATExempt>True</VATExempt>

</Item3>


-<Item4>

<Code>9146163</Code>

<Desc>someDes</Desc>

<Price>888.2017</Price>

<VATExempt>False</VATExempt>

</Item4>


-<Item5>

<Code>2107745</Code>

<Desc>someDes</Desc>

<Price>958.7421</Price>

<VATExempt>True</VATExempt>

</Item5>


-<Item6>

<Code>7266565</Code>

<Desc>someDes</Desc>

<Price>461.4441</Price>

<VATExempt>False</VATExempt>

</Item6>


-<Item7>

<Code>2509865</Code>

<Desc>someDes</Desc>

<Price>247.8107</Price>

<VATExempt>True</VATExempt>

</Item7>


-<Item8>

<Code>4654564</Code>

<Desc>someDes</Desc>

<Price>195.5203</Price>

<VATExempt>False</VATExempt>

</Item8>


-<Item9>

<Code>6787421</Code>

<Desc>someDes</Desc>

<Price>864.1899</Price>

<VATExempt>False</VATExempt>

</Item9>


-<Item10>

<Code>2689976</Code>

<Desc>someDes</Desc>

<Price>906.9549</Price>

<VATExempt>False</VATExempt>

</Item10>

</Items>

</Order>

Root: Order
Sibling: OrdNum
Child Of Sibling: #text
Sibling: Date
Child Of Sibling: #text
Sibling: Customer
Child Of Sibling: FName
Child Of Child of Sibling: #text
java.lang.NullPointerException
    at rXML.main(rXML.java:37)
¿Fue útil?

Solución

In the innermost for :

h < n2.getChildNodes().getLength();

is wrong.

It should be

h < n2.getChildNodes().item(j).getChildNodes().getLength();

Otros consejos

Answer already posted so i wont repeat it. But just as a helper when dealing with similar problems in the future.

Avoid using long trains like this:

n2.getChildNodes().item(j).getChildNodes().item(h).getNodeName()

Instead separate the calls on separate lines. This will give a better hint at where the exception is occurring. In the code above each call beyond the first can be the possible culprit of the nullpointer. n2.getChildNodes().item(j) would already have thrown a nullpointer in this case though but there are still multiple possible sources returning null.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top