title: Google search via Java Api - Multiple requests
body: <p>I'm coming from <a href="https://stackoverflow.com/questions/3727662/how-can-you-search-google-programmatically-java-api">this question</a>.</p>
  
  <p>The following code does not work well:</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
      for (int i = 0; i &lt; 15; i++)
      {
          String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&amp;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());
      }
  }
  </code></pre>
  
  <p>The search query works fine if I run it one time, however in this loop I get a null pointer exception.</p>
  
  <p>Unfortunately I need my program to make several queries :( What can I do?</p>
  
  <p>It returns a <code>NullPointerException</code> at the first <code>results.getResponseData</code>.</p>