Lee Armstrong
Lee Armstrong

Reputation: 11452

NullPointerException with StringBuilder

I get a very common crash below from the code below.

I thought my try, catches will have handled that.

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1102)
Caused by: java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:65)
at java.io.InputStreamReader.<init>(InputStreamReader.java:65)
at com.test.test.FinderMain.grabPlaneRoute(FinderMain.java:759)
at com.test.test.FinderMain.access$7(FinderMain.java:729)
at com.test.test.FinderMain$GetRouteTask.doInBackground(FinderMain.java:684)
at com.test.test.FinderMain$GetRouteTask.doInBackground(FinderMain.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

Line 759 is within grabPlaneRoute and is the line containing the "StringBuilder total = new StringBuilder();"

Can someone help with this, it is driving me crazy! :-)

private void grabPlaneRoute(String adshex) {


    List<GeoPoint> localList = new ArrayList<GeoPoint>();

    HttpURLConnection con = null;
    URL url;
    InputStream is=null;
    try {
        url = new URL("http://www.testurl.com&test=" + adshex);

        con = (HttpURLConnection) url.openConnection();
        con.setReadTimeout(20000 /* milliseconds */);
        con.setConnectTimeout(60000 /* milliseconds */);
        con.setRequestMethod("GET");
        con.setDoInput(true);

        // Start the query
        con.connect();
        is = con.getInputStream();
    }catch (IOException e) {
                    //handle the exception !
        e.printStackTrace();
        return;
    }

    //localList = decodePoly(convertStreamToString(is));
    //Log.i("HTTP DUMP", convertStreamToString(is));

    BufferedReader r = new BufferedReader(new InputStreamReader(is),8024);
    StringBuilder total = new StringBuilder();
    String line;
     try {
    while ((line = r.readLine()) != null) {
        String[] separated = line.split(",");

        GeoPoint p = getPoint(Double.valueOf(separated[0]),Double.valueOf(separated[1]));
        localList.add(p);
    }
     }catch (IOException e) {
         //handle the exception !
            e.printStackTrace(); 
     }



    drawPlaneRoute(localList);


}

Upvotes: 0

Views: 2293

Answers (2)

Michael Petrotta
Michael Petrotta

Reputation: 60902

Line 759 is actually the line that begins with BufferedReader r = new ....

The variable is is null.

What happens when con.GetInputStream returns a null, and doesn't throw an exception? You're not handling that case, and that's likely what's happening here.

In addition, I don't see a need for the either try/catch block at all. Just let the exception percolate up the call stack. You're hiding the IOException now, preventing anyone calling grabPlaneRoute from discovering that an error occurred.

Upvotes: 4

Cristian
Cristian

Reputation: 200080

It's fairly impossible for StringBuilder total = new StringBuilder(); to cause a NullPointerException. On the other hand, the nearest cause of the Exception is in the logtrace:

at java.io.Reader.<init>(Reader.java:65)
at java.io.InputStreamReader.<init>(InputStreamReader.java:65)

Which makes me think that it's caused by:

BufferedReader r = new BufferedReader(new InputStreamReader(is),8024);

In that case, the only thing that could be null is is.

Upvotes: 2

Related Questions