0

I have tool code use Google search API.

My code:

import com.google.gson.Gson;

class GoogleResults {

    private ResponseData responseData;

    public ResponseData getResponseData() {
        return responseData;
    }

    public void setResponseData(ResponseData responseData) {
        this.responseData = responseData;
    }

    public String toString() {
        return "ResponseData[" + responseData + "]";
    }

    static class ResponseData {
        private List<Result> results;

        public List<Result> getResults() {
            return results;
        }

        public void setResults(List<Result> results) {
            this.results = results;
        }

        public String toString() {
            return "Results[" + results + "]";
        }
    }

    static class Result {
        private String url;

        public String getUrl() {
            return url;
        }


        public void setUrl(String url) {
            this.url = url;
        }


        public String toString() {
            return "Result[url:" + url + " ]";
        }
    }
}

public class CrawData {

    public static void main(String[] args) throws IOException, InterruptedException {

        String query;
        int n;
        int k=0;
        String site;
        String resultset;
        Scanner st = new Scanner(System.in);
        System.out.print(" Input key search: ");
        query = st.nextLine();

        System.out.print("Input site: ");
        site = st.nextLine();

        System.out.print("Input number of result: ");
        n = st.nextInt();

        resultset = query + " site:" + site;


        for (int j = 0; j < n; j = j + 1) {

            Thread.sleep(4000);
            String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start="+j+"&q=";
            String charset = "UTF-8";



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

            int total = results.getResponseData().getResults().size();

            // Show title and URL of each results
            for (int i = 0; i <= total - 1; i++) {

                String Url = results.getResponseData().getResults().get(i)
                        .getUrl();
                k = k+1;
                System.out.println("URL: " +Url+ " " + k);

            }
        }
    }
}

when i run it, i have trouble about result return of code.

My system return list url of website.. but it not stable.

Some picture:

my error

Have error: Exception in thread "main" java.lang.NullPointerException at CrawData.main(CrawData.java:107)

help me...

Sorry my english is too bad.. :(

matiash
  • 54,791
  • 16
  • 125
  • 154

1 Answers1

0

My guess is that on this line:

String Url = results.getResponseData().getResults().get(i)
                        .getUrl();

Either get(i) is returning null or getUrl() is returning null. You should add some error handling logic:

if (results.getResponseData().getResults().get(i) != null && 
results.getResponseData().getResults().get(i).getUrl() !=null) {
  String Url = results.getResponseData().getResults().get(i)
                       .getUrl();
  k = k+1;
  System.out.println("URL: " +Url+ " " + k);
} else {
  // Print some type of error here.  Try to figure out why the result or the 
  // url is null
}
Khary Mendez
  • 1,818
  • 1
  • 14
  • 18
  • I've do ​​your way, but my system show same message error. Website require 10 post to post picture. I not enough. You can see : Some picture: on my post... – KinyStar Jul 23 '14 at 01:36
  • This api is actually deprecated and now somewhat limited, see http://stackoverflow.com/questions/3727662/how-can-you-search-google-programmatically-java-api – Khary Mendez Jul 23 '14 at 01:39