Search code examples
javajsonxmlorg.jsonweb-developer-toolbar

Exception in thread "main" java.lang.NullPointerException at java.io.StringReader.<init>(Unknown Source)


I am trying to convert my Xml string into an json object. I am using the org.Json package. I have requested information from the wolfram alpha server. The server give back xml and i want to used the org.json package to convert it to json. The method I am trying to use is static which lies inside the XML class. I created a JSONObject method thinking that i would convert but i keep getting an error message.

Here is my code from my main method.

import java.net.*;
import java.io.*;
import org.json.*;
import java.net.URLConnection;
import java.util.Scanner;
import java.net.URL;
import javax.swing.*;


public class Test {

public static void main(String[] args)throws IOException,   JSONException{//Beginning of class
    // TODO Auto-generated method stub
    String appID = "YWT4UP-Y9W7AREAHJ";
    String search = "bird";

    URL wolframData = new URL("http://api.wolframalpha.com/v2/query?input="+search+"&appid="+appID);

    URLConnection connection = wolframData.openConnection();

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    //reads in the data
    String xmlDoc;

    while((xmlDoc = in.readLine()) != null) //converts buffer reader to string
    System.out.println(xmlDoc);
    in.close();

   JSONObject jsonDoc = (JSONObject) XML.toJSONObject(xmlDoc);



}//End of method

}//End of class

Here is the error message that is displayed when it reach the code that converts the xml to json:

Exception in thread "main" java.lang.NullPointerException
at java.io.StringReader.<init>(Unknown Source)
at org.json.JSONTokener.<init>(JSONTokener.java:85)
at org.json.XMLTokener.<init>(XMLTokener.java:55)
at org.json.XML.toJSONObject(XML.java:329)
at Test.main(Test.java:31)

Solution

  • I think the answer should be ,

    import java.net.*;
        import java.io.*;
        import org.json.*;
        import java.net.URLConnection;
        import java.util.Scanner;
        import java.net.URL;
        import javax.swing.*;
    
    
        public class Test {
    
        public static void main(String[] args)throws IOException,   JSONException{//Beginning of class
            // TODO Auto-generated method stub
            String appID = "YWT4UP-Y9W7AREAHJ";
            String search = "bird";
    
            URL wolframData = new URL("http://api.wolframalpha.com/v2/query?input="+search+"&appid="+appID);
    
            URLConnection connection = wolframData.openConnection();
    
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            //reads in the data
            String xmlDoc;
        StringBuilder sb = new StringBuilder();
            while((xmlDoc = in.readLine()) != null) { //converts buffer reader to string
            System.out.println(xmlDoc);
        sb.append(xmlDoc);
        }
            in.close();
    
           JSONObject jsonDoc = (JSONObject) XML.toJSONObject(sb.toString());
    
    
    
        }//End of method
    
        }//End of class