3

I'm coming from this question.

The following code does not work well:

public static void main(String[] args) throws Exception {
    for (int i = 0; i < 15; i++)
    {
        String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
        String search = "test";
        String charset = "UTF-8";

        URL url = new URL(google + URLEncoder.encode(search, charset));
        Reader reader = new InputStreamReader(url.openStream(), charset);
        GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);

        // Show title and URL of 1st result.
        System.out.println(results.getResponseData().getResults().get(0).getTitle());
        System.out.println(results.getResponseData().getResults().get(0).getUrl());
    }
}

The search query works fine if I run it one time, however in this loop I get a null pointer exception.

Unfortunately I need my program to make several queries :( What can I do?

It returns a NullPointerException at the first results.getResponseData.

Community
  • 1
  • 1
David
  • 15,652
  • 26
  • 115
  • 156
  • i am also facing this problem. So how to get rid of that? – Akhilesh Dhar Dubey Oct 28 '13 at 21:13
  • you can't. It's against Google's TOS. You could try adding a longer delay though, or try sending IPs to Google. – David Oct 29 '13 at 21:24
  • i have added ip also. but still not working. `String address = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&start=0&userip=14.90.136.150&safe=active&rsz=8&q=";` sometime it do't give any exception and search the text from google but most of the time it throws `NullPointerException`. why? – Akhilesh Dhar Dubey Oct 30 '13 at 16:29

2 Answers2

2

This is happening because Google actively blocks suspected terms of service abuse. See section 5.3 here:

http://www.google.com/accounts/TOS

If Google detects that you are issuing search requests via a program without their consent, they don't send back results. Your JSON response will contain this:

{"responseData": null, "responseDetails": "Suspected Terms of Service Abuse. Please see http://code.google.com/apis/errors", "responseStatus": 403}
khill
  • 577
  • 4
  • 5
0

Check to make sure results and other contained objects are not null before you use them.

if ((results != null) && (results.getResponseData() != null) &&
    (results.getResponseData().getResults() != null) &&
    (results.getResponseData().getResults().get(0) != null)) {
    // Show title and URL of 1st result.
    System.out.println(results.getResponseData().getResults().get(0).getTitle());
    System.out.println(results.getResponseData().getResults().get(0).getUrl());
}
Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
  • 1
    Well and good. What I'm asking is - why is results NULL in the first place? And how can I avoid it. – David Nov 16 '11 at 17:40