0

I have no clue what's happening, so I will have to ask this here.

I have an Activity that returns a location as LatLng. I then check if it's null before I pass it to my moveCamera() method that's supposed to show it on a map.

if(location != null) {
        mapActivity.moveCamera(location);
    }else{
        Toast.makeText(MainActivity.this, "Couldn't determine location",  Toast.LENGTH_SHORT).show();
    }

But although I check for null, when i run it, i get:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void de.ratchetcoding.theotherside.MapsActivity.moveCamera(com.google.android.gms.maps.model.LatLng)' on a null object

Any help is appreciated. If you need the source code tell me, I can upload it to github.

  • 3
    `mapActivity == null` – Mohammed Aouf Zouag Jan 09 '16 at 12:55
  • refer http://stackoverflow.com/questions/25653898/google-maps-fragment-returning-null-inside-a-fragment – sasikumar Jan 09 '16 at 12:56
  • Sorry, i didn't think about the possibilty that mapsActivity is null, i was too focused on location. I added `if(location != null && mapActivity != null)`. The code now works and the statements in the if block gets executed, can someone explain me why that is? Because if mapsActivity was null before, shouldn't it still be null and the if block not get executed? – strange-matter Jan 09 '16 at 13:01

1 Answers1

0

This seems due to your map activity is null . Put check for mapActivity is null or not.