views:

83

answers:

2

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&amp;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);


}
+4  A: 

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.

Michael Petrotta
Sigh... I hate when I answer the same than other user some seconds after. So, I think your is correct, so +1!
Cristian
@Christian: well, yours is more complete, so +1 right back at you.
Michael Petrotta
+2  A: 

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.

Cristian
Got ya, I suppose then a if != null there and miss that section of code?
Lee Armstrong

related questions