0

I am reading XML data from a web service and storing it in an String:

String output;
String dataReceived = "";
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
       dataReceived = dataReceived + output;
}

But when I try to create a DOM Document object from the String, I get a NPE. I am following this examples:

Option 1: How do I load an org.w3c.dom.Document from XML in a string?

Option 2: In Java, how do I parse XML as a String instead of a file?

public void parseXML(String dataReceived) {

//Option 1
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(dataReceived.getBytes()));
System.out.println(document);


//Option 2
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(dataReceived));
Document document = builder.parse(is);
System.out.println(document);

}

But as I said I get a NPE. Why?

[#document: null]
java.lang.NullPointerException

UPDATE: I print the received String at the beginning of parseXML function and this is what I get

Received data: <?xml version="1.0" encoding="UTF-8"?><GeocodeResponse> <status>OK</status> <result> <type>street_address</type> <formatted_address>Calle de José Abascal, 1, 28003 Madrid, Spain</formatted_address> <address_component> <long_name>1</long_name> <short_name>1</short_name> <type>street_number</type> </address_component> <address_component> <long_name>Calle de José Abascal</long_name> <short_name>Calle de José Abascal</short_name> <type>route</type> </address_component> <address_component> <long_name>Madrid</long_name> <short_name>Madrid</short_name> <type>locality</type> <type>political</type> </address_component> <address_component> <long_name>Madrid</long_name> <short_name>M</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> <address_component> <long_name>Comunidad de Madrid</long_name> <short_name>Comunidad de Madrid</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>Spain</long_name> <short_name>ES</short_name> <type>country</type> <type>political</type> </address_component> <address_component> <long_name>28003</long_name> <short_name>28003</short_name> <type>postal_code</type> </address_component> <geometry> <location> <lat>40.4387423</lat> <lng>-3.7022840</lng> </location> <location_type>ROOFTOP</location_type> <viewport> <southwest> <lat>40.4373933</lat> <lng>-3.7036330</lng> </southwest> <northeast> <lat>40.4400913</lat> <lng>-3.7009350</lng> </northeast> </viewport> </geometry> <partial_match>true</partial_match> <place_id>ChIJP58jD_YoQg0RPwBTvmkE2qQ</place_id> </result></GeocodeResponse>

UPDATE 2:

As minus has said that is not the part that launches the NPE. It is the following code. I have thought as when printing System.out.println(document); it shows null, maybe the parse was erroneous. I will take a look at this code. Thanks.

 XPath xPath = XPathFactory.newInstance().newXPath();

   Node nodeLat = (Node) xPath.evaluate("//geometry/location/lat",
                        document, XPathConstants.NODE);
   Node nodeLng = (Node) xPath.evaluate("//geometry/location/lng",
                        document, XPathConstants.NODE);

   System.out.println("n " + nodeLat);
   latLong[0] = Double.parseDouble(nodeLat.getNodeValue());
   latLong[1] = Double.parseDouble(nodeLng.getNodeValue());

UPDATE 3

Just BTW The correct way to read the node's value is:

String expression = "//geometry/location/lat";
Node node = (Node) xPath.compile(expression).evaluate(document, XPathConstants.NODE);
System.out.println("latitude: " + node.getTextContent());

And thanks again

10
  • 1
    Have you printed out dataReceived to verify it's populated? If it is, try just translating that to XML via a file and see if it's valid. Commented May 8, 2017 at 19:46
  • @Dan W, yes, the String is populated. OK, I will try Commented May 8, 2017 at 19:47
  • give an example of input that fails. Commented May 8, 2017 at 19:59
  • @njzk Updated with an exmple of a String that fails Commented May 8, 2017 at 20:07
  • 1
    By the way; getBytes(StandardCharsets.UTF_8). Looks like debugging is needed. Check that you close things too, check the line of the NPE; is it really on document. Commented May 8, 2017 at 20:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.