Error reading from xml file in java

i have the following xml:


and i want to get values from nodes for example:
the node Y1 has the element v-13.0

i used this method:

Map<String, Double> m = new HashMap<String, Double>();
File xmlFile = new File("Data.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document xmlDoc = docBuilder.parse(xmlFile);

NodeList list = xmlDoc.getElementsByTagName("properties");

Node node1 = list.item(0);
Element element1 = (Element) node1;
String s = element1.getElementsByTagName("Y1").item(0).getTextContent();
String[] temp ;//= s.split("[-]");
m.put("Y1", Double.parseDouble(temp[1]));

s =element1.getElementsByTagName("X1").item(0).getTextContent();
temp = s.split("[-]");
m.put("X1", Double.parseDouble(temp[1]));

s =element1.getElementsByTagName("Y2").item(0).getTextContent();
temp = s.split("[-]");
m.put("Y2", Double.parseDouble(temp[1]));

s =element1.getElementsByTagName("X2").item(0).getTextContent();
temp = s.split("[-]");
m.put("X2", Double.parseDouble(temp[1]));

when i used this method to get elements of the X1, Y1, X2, Y2 in properties tag it gives me null pointer exception in the line String s = element1.getElementsByTagName("Y1").item(0).getTextContent() but when i used the same method to get elements of X, Y in the position tag it worked.
what is the solution?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s