DrkStr
DrkStr

Reputation: 1932

NullPointerException in my AsyncTask

Hey everyone , I have an Async Task for pointing to a point on the map based on user inputted address. I have a NullPointerException in the onPostExecute and I can work out why. Here is the async class

     private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{

            @Override
            protected List<Address> doInBackground(String... locationName) {
                // Creating an instance of Geocoder class
                Geocoder geocoder = new Geocoder(getBaseContext());
                List<Address> addresses = null;

                try {
                    // Getting only one address that matches the query
                    addresses = geocoder.getFromLocationName(locationName[0], 3);
                } catch (IOException e) {
                    e.printStackTrace();
                }           
                return addresses;
            }


            @Override
            protected void onPostExecute(List<Address> addresses) {         

                if(addresses==null || addresses.size()==0){
                    Toast.makeText(getBaseContext(), "No Location found.Please check address", Toast.LENGTH_SHORT).show();
                }

                // Clears all the existing markers on the map
                mMap.clear();

                // Adding Markers on Google Map for each matching address
                for(int i=0;i<addresses.size();i++){// this is line 585 from the logcat             

                    Address address = (Address) addresses.get(i);

                    // Creating an instance of GeoPoint, to display in Google Map
                    LatLng latLng_search = new LatLng(address.getLatitude(), address.getLongitude());

                     String addressText = String.format("%s, %s",
                                address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                                address.getLocality());

                        MarkerOptions markerOptions = new MarkerOptions();
                        markerOptions.position(latLng_search);
                        markerOptions.title(addressText);

                        mMap.addMarker(markerOptions);


                    // Locate the first location
                    if(i==0)                        
                        mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng_search));   
                }           
            }               
        }  

here is the logcat:

 09-28 02:51:45.501: E/AndroidRuntime(16587): FATAL EXCEPTION: main
 09-28 02:51:45.501: E/AndroidRuntime(16587): java.lang.NullPointerException
 09-28 02:51:45.501: E/AndroidRuntime(16587):   at drkstr.yar.Create_reminder_loc$GeocoderTask.onPostExecute(Create_reminder_loc.java:585)
 09-28 02:51:45.501: E/AndroidRuntime(16587):   at drkstr.yar.Create_reminder_loc$GeocoderTask.onPostExecute(Create_reminder_loc.java:1)
 09-28 02:51:45.501: E/AndroidRuntime(16587):   at android.os.AsyncTask.finish(AsyncTask.java:417)
 09-28 02:51:45.501: E/AndroidRuntime(16587):   at android.os.AsyncTask.access$300(AsyncTask.java:127)
 09-28 02:51:45.501: E/AndroidRuntime(16587):   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
 09-28 02:51:45.501: E/AndroidRuntime(16587):   at android.os.Handler.dispatchMessage(Handler.java:99)
 09-28 02:51:45.501: E/AndroidRuntime(16587):   at android.os.Looper.loop(Looper.java:143)
 09-28 02:51:45.501: E/AndroidRuntime(16587):   at android.app.ActivityThread.main(ActivityThread.java:4196)
 09-28 02:51:45.501: E/AndroidRuntime(16587):   at java.lang.reflect.Method.invokeNative(Native Method)
 09-28 02:51:45.501: E/AndroidRuntime(16587):   at java.lang.reflect.Method.invoke(Method.java:507)
 09-28 02:51:45.501: E/AndroidRuntime(16587):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 09-28 02:51:45.501: E/AndroidRuntime(16587):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 09-28 02:51:45.501: E/AndroidRuntime(16587):   at dalvik.system.NativeStart.main(Native Method)

The error is turning up at line 585 , I have marked this in the code. The only 2 variables in that line are i and address and I check if address in null. I don't get why I am still getting a null pointer exception.

Let me know if you need to see anymore of the code.

Thanks for taking the time to read this and for any help that you can give.

Upvotes: 0

Views: 382

Answers (3)

Manishika
Manishika

Reputation: 5564

Put your for loop in else part because right now even if your addresses is null or address.size=0 the for loop gets executed with the null value and gives NPE

Upvotes: 1

betorcs
betorcs

Reputation: 2821

if(addresses==null || addresses.size()==0){
      Toast.makeText(getBaseContext(), "No Location found.Please check address", Toast.LENGTH_SHORT).show();
      return; // add this
}

Upvotes: 2

Birb
Birb

Reputation: 866

As suggested in the comment, your addresses variable is most likely null.

You check if addresses is null in the conditional statement, but you never actually handle this situation. If addresses is null all you do is notify the user through a Toast, but a FRACTION of a second later (before the animation of the Toast is even loaded on your screen) your program crashes.

You need to handle the situation where addresses is null (that is, not call methods on the variable and try to populate the variable, or something completely different), or make sure it never is null.

Upvotes: 1

Related Questions