gpt4 book ai didi

java - 将 JAVA String 转换为 JSON 对象时出现 NullPointerException

转载 作者:搜寻专家 更新时间:2023-11-01 03:36:45 26 4
gpt4 key购买 nike

我正在编写一个 Java Web 应用程序来使用 Twitter API 检索推文。我有一个搜索方法并试图在 jsp 页面调用该方法。

public class SearchTwitter {

private static final String CONSUMER_KEY = "*************";
private static final String CONSUMER_SECRET = "****************";
private static final String OAUTH_TOKEN = "*********************";
private static final String OAUTH_TOKEN_SECRET = "*****************";

public String Search(String query) throws Exception {
OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY,
CONSUMER_SECRET);
consumer.setTokenWithSecret(OAUTH_TOKEN, OAUTH_TOKEN_SECRET);
query = query.replaceAll(" ", "%20");
String twitterURL = "https://api.twitter.com/1.1/search/tweets.json?q=" + query + "&src=typd";
URL url = new URL(twitterURL);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.setDoOutput(true);
request.setRequestProperty("Content-Type", "application/json");
request.setRequestProperty("Accept", "application/json");
request.setRequestMethod("GET");
consumer.sign(request);
request.connect();
StringBuilder text = new StringBuilder();
InputStreamReader in = new InputStreamReader((InputStream) request.getContent());
BufferedReader buff = new BufferedReader(in);
System.out.println("Getting data ...");
String line;
do {
line = buff.readLine();
text.append(line);
} while (line != null);
String strResponse = text.toString();
return strResponse;
}

Search 方法返回一个字符串

            <%
SearchTwitter stw = new SearchTwitter();
String tweet = request.getParameter("query");

JSONObject result = new JSONObject(stw.Search(tweet));
JSONArray statuses = result.getJSONArray("statuses");
for (int i = 0; i < statuses.length(); i++) {
String time = statuses.getJSONObject(i).getString("created_at");
String user = statuses.getJSONObject(i).getJSONObject("user").getString("screen_name");
String text = statuses.getJSONObject(i).getString("text");
System.out.println(time.toString());
System.out.println(user.toString());
System.out.println(text.toString());
}
%>

我正在尝试将字符串转换为 JSON 对象,但它显示 HTTP 500 NullPointerException 错误我不知道我哪里出错了,因为我是 JAVA 的新手。有人可以帮忙吗?非常感谢!

最佳答案

你的错误很可能发生是因为

String tweet = request.getParameter("query");

返回null,即找不到参数,返回null

之后您尝试在以下位置对 null 执行操作:

query = query.replaceAll(" ", "%20");

但是因为 null 不能执行 .replaceAll 它抛出一个 NullPointerException

有不同的方法来处理这个问题:

例如,您可以在检索 tweet 变量后立即检查 null,如下所示:

    <%
SearchTwitter stw = new SearchTwitter();
String tweet = request.getParameter("query");

if(tweet!=null){
JSONObject result = new JSONObject(stw.Search(tweet));
JSONArray statuses = result.getJSONArray("statuses");
for (int i = 0; i < statuses.length(); i++) {
String time = statuses.getJSONObject(i).getString("created_at");
String user = statuses.getJSONObject(i).getJSONObject("user").getString("screen_name");
String text = statuses.getJSONObject(i).getString("text");
System.out.println(time.toString());
System.out.println(user.toString());
System.out.println(text.toString());
}
} else {
System.out.println("Unable to retrieve query!");
}
%>

因此,如果 tweet 变量出现 null,那么您将打印“Unable to retrieve query!”,而不是时间用户和文本。等等

关于java - 将 JAVA String 转换为 JSON 对象时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29302021/

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