4

I'm writing a java web application to retrieve tweets by using Twitter API. I got a Search method and trying to call the method at jsp page.

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;
}

}

The Search method returns a string

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

And i'm trying to convert the String to JSON object, but it shows HTTP 500 NullPointerException error I don't know where i'm getting wrong because i'm new to JAVA. Could anybody help? Really appreciate!

4
  • 6
    Put up a stacktrace and show which line of code it points to Commented Mar 27, 2015 at 13:39
  • Just wondering: what makes you think that writing a java web application is a good starting point for a Java newbie? I mean: if you want to learn the language; why don't you start with tutorials/books that especially target newbies? If you are not doing this to learn java - why are you using java then (instead of a language in which you are more skilled?) Commented Mar 27, 2015 at 13:42
  • query = query.replaceAll(" ", "%20"); It points to this line Commented Mar 27, 2015 at 13:43
  • I know it is not a good starting point for Java... but this is one of my uni class i'm taking. Seems it is little bit hard for me :( Commented Mar 27, 2015 at 13:48

1 Answer 1

1

Your error most likely happens because

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

returns null, that is, it cannot find the parameter and returns null.

After that you attempt to perform actions on null at:

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

but since null cannot perform .replaceAll it throws a NullPointerException

There are different ways to deal with this:

For example, you could check for null right after retrieving the tweet variable like so:

    <%
        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!");
        }
    %>

So if the tweet variable comes up null then instead of time user and text you'll print "Unable to retrieve query!" etc.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes but i got an input text form in jsp page, and query is the name. <input type="text" name="query" placeholder="Enter your keyword"> why would this error happened since i have no chance to give input. It throws NullPointerException directly.
I got those tweets!! Thank you so much. But i am still wondering why the results are retrieved after an if check. Why it returns null without check
That is strange... But glad you got it working in the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.