从Java访问Servlet响应时出现NullPointerException

时间:2011-12-02 18:37:03

标签: java

我从Java文件中调用Java servlet。 servlet文件具有SOP以返回JSON字符串。现在我希望在我的Java代码中使用该字符串,然后将其解析为XML。

我的代码如下:

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();

问题是我的Java代码SOP是:

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

问题是我没有在inputLine中获得完整的输出,另一个问题是NullPointerException。我无法弄清楚为什么会这样。

3 个答案:

答案 0 :(得分:4)

你正在循环这样做:

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

要终止循环,inputLine必须为null,所以当你到达时:JSONObject js = new JSONObject(inputLine); 你会得到一个空指针异常。

答案 1 :(得分:1)

看看这是否更好:

        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();

答案 2 :(得分:1)

看看你在这做什么:

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

您一直在阅读,直到 inputLine为空。你然后打电话:

JSONObject js = new JSONObject(inputLine);

所以你的代码是有效的:

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

据推测,这就是问题所在。所以,问题是:你在哪里想要循环?