gpt4 book ai didi

java - 从 Java 访问 Servlet Response 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 16:36:35 25 4
gpt4 key购买 nike

我正在从 Java 文件调用 Java servlet。 servlet 文件具有返回 JSON 字符串的 SOP。现在我希望在 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。我无法弄清楚为什么会发生这种情况。

最佳答案

你的循环正在这样做:

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

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

关于java - 从 Java 访问 Servlet Response 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8360977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com