typedefcoder2
typedefcoder2

Reputation: 300

NullPointerException while accessing Servlet Response from Java

I am calling a Java servlet from my Java file. The servlet file has SOP to return a JSON string. Now I wish to use that string in my Java code and then parse it into XML.

My code is as follows:

URL urlS = new URL (servletURL);
URLConnection urlc;
urlc = urlS.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

String inputLine;
while ((inputLine = in.readLine()) != null)
    System.out.println("We got JSON Buddy: "+inputLine);
JSONObject js = new JSONObject(inputLine);
System.out.println("We got json object js: "+js.toString());
in.close();

The problem is my SOP for Java code is :

12-02 10:30:12.254: I/System.out(24169): We got JSON Buddy: {"movies": { "total": 19, "movie":[{"cover":"http://content9.flixster.com/movie/11/16/04/11160419_pro.jpg","tile":"A Very Harold & Kumar 3D Christmas","MovieDuration":"(R , 1 hr. 29 min.)","showtime":"                                                                                                                                                                                                                                                                                                                                                                                                                                                                           9:35 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         12:00 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                ","theatre":"Rave Motion Pictures 18 + IMAX","url":"http://www.flixster.com/movie/a-very-harold-and-kumar-christmas"},{"cover":"http://content6.flixster.com/movie/11/16/12/11161272_pro.jpg","tile":"Arthur Christmas 3D","MovieDuration":"(PG , 1 hr. 37 min.)","showtime":"                                                                                                                                                                                                                                                                                                                                                                                                                                                                           12:15 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         3:00 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         5:35 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                ","theatre":"Rave Motion Pictures 18 + IMAX","url":"http://www.flixster.com/movie/arthur-christ
12-02 10:30:12.266: I/System.out(24169): Exception: java.lang.NullPointerException

The issue is I am not getting the complete output in the inputLine, and the other problem is the NullPointerException. I am not able to figure out why this is happening.

Upvotes: 0

Views: 193

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1499810

Look at what you're doing here:

while ((inputLine = in.readLine()) != null)
    System.out.println("We got JSON Buddy: "+inputLine);
JSONObject js = new JSONObject(inputLine);

You're keeping reading until inputLine is null. You're then calling:

JSONObject js = new JSONObject(inputLine);

So your code is effectively:

while ((inputLine = in.readLine()) != null)
    System.out.println("We got JSON Buddy: "+inputLine);
JSONObject js = new JSONObject(null);

Presumably that's the problem. So, the question is: where did you want to loop?

Upvotes: 1

duffymo
duffymo

Reputation: 308733

See if this is better:

        URL urlS = new URL (servletURL);
        URLConnection urlc;
        urlc = urlS.openConnection();
        BufferedReader in = new BufferedReader(new         InputStreamReader(urlc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            System.out.println("We got JSON Buddy: "+inputLine);
            JSONObject js = new JSONObject(inputLine);            
            System.out.println("We got json object js: "+js.toString());
        }
        in.close();

Upvotes: 1

Chris
Chris

Reputation: 23171

You're loop is doing this:

  while ((inputLine = in.readLine()) != null)
                System.out.println("We got JSON Buddy: "+inputLine);

For the loop to terminate, inputLine has to be null so when you get here: JSONObject js = new JSONObject(inputLine); you will get a null pointer exception.

Upvotes: 4

Related Questions