将JAVA字符串转换为JSON对象时获取NullPointerException

将JAVA字符串转换为JSON对象时获取NullPointerException,java,json,Java,Json,我正在编写一个java web应用程序,通过使用Twitter API检索推文。我得到了一个搜索方法,并试图在jsp页面调用该方法 public class SearchTwitter { private static final String CONSUMER_KEY = "*************"; private static final String CONSUMER_SECRET = "****************"; private static final String

我正在编写一个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;
}
}

搜索方法返回一个字符串

            <%
            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
,那么您将打印“无法检索查询!”等,而不是时间用户和文本。

显示堆栈跟踪并显示它指向哪行代码只是想知道:是什么让您认为编写java web应用程序是java新手的良好起点?我的意思是:如果你想学习语言;你为什么不从专门针对新手的教程/书籍开始呢?如果您这样做不是为了学习java,那么为什么要使用java(而不是您更熟练的语言?)query=query.replaceAll(“,“%20”);它指向这条线我知道这不是Java的好起点。。。但这是我的大学课程之一。似乎这对我来说有点难:(是的,但是我在jsp页面中得到了一个输入文本表单,
query
是名称。
为什么会发生这个错误,因为我没有机会输入。它直接抛出
NullPointerException
。我收到了这些推文!!非常感谢。但是我仍然想知道为什么在
if
检查后会检索结果。为什么它返回
null
而不进行检查,这很奇怪……但很高兴您最终让它工作了。