0

Getting a xml document from the http url, and printing of the documents works perfect (System.out.println(xmlResult)), but when I convert string into xml Document it produces an Null Pointer exception

Java code

public static void main(String[] args) throws MalformedURLException, IOException, ParserConfigurationException, SAXException {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        URL url = new URL("http://ops.epo.org/3.1/rest-services/published-data/publication/epodoc/US7039403/biblio");

        HttpURLConnection epo = (HttpURLConnection) url.openConnection();
        epo.setRequestMethod("GET");
        epo.setRequestProperty("ContentType", "text/xml;charset=\"utf-8\"");
        epo.setRequestProperty("Accept", "text/xml");

        InputStream inputstream = epo.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputstream,"UTF-8"));

            String xmlResult = "";
            while ((xmlResult = bufferedReader.readLine()) != null) {                
            }           
            InputSource inputSource = new InputSource();
            inputSource.setCharacterStream(new StringReader(xmlResult));
            Document xmlDocument = documentBuilder.parse(inputSource);

            System.out.println("Root element :" + xmlDocument.getDocumentElement().getNodeName());        
    }

Exception details

Exception in thread "main" java.lang.NullPointerException
    at java.io.StringReader.<init>(StringReader.java:50)
    at ExtractBiblio.Teser.main(Teser.java:62) 
Line:inputSource.setCharacterStream(new StringReader(xmlResult));

help me to resolve it.

Prabu
  • 3,550
  • 9
  • 44
  • 85

0 Answers0