1

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)
Ellipse
  • 27
  • 2
  • 9
  • 3
    ```while((xmlDoc = in.readLine()) != null)``` Eh... So ```xmlDoc``` will always be ```null``` at the end of the loop. – Jorn Vernee Apr 08 '16 at 20:15
  • @jornVernee So how does that affect the program. Can you explain what you mean in more detail? Do you have a solution? – Ellipse Apr 08 '16 at 20:31
  • See: [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Jorn Vernee Apr 08 '16 at 21:11

1 Answers1

1

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
Lakmal Vithanage
  • 2,767
  • 7
  • 42
  • 58