0

I am trying to implement a GoogleMap object into my code. When I use getMapASync() and assign the map created in the onMapReady() call back to the GoogleMap object I want to use, it works within that method, but once I refer to it outside of that method it says that it is still null. Why? public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

GoogleMap mMap;
private static final int ERROR_DIALOG_REQUEST = 9001;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(servicesOK()) {
        setContentView(R.layout.activity_map);
        if (initMap()) {
            Toast.makeText(this, "Ready to map", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Map not connected", Toast.LENGTH_SHORT).show();

        }
    }
    else{
        setContentView(R.layout.activity_main);
    }
String mapType2 = Integer.toString(mMap.getMapType());
    Toast.makeText(this, mapType2 + " mMap", Toast.LENGTH_SHORT).show(); 
//This is for debugging purposes
}


public boolean servicesOK(){
    int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if(isAvailable == ConnectionResult.SUCCESS){
        return true;
    }
    else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)){
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,this,ERROR_DIALOG_REQUEST);
        dialog.show();
    }
    else{
        Toast.makeText(this, "Can't connect to mapping service", Toast.LENGTH_SHORT).show();
    }

    return false;
}

private boolean initMap(){
    if(mMap == null){
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    return (mMap != null);
}
@Override
public void onMapReady(GoogleMap map) {
    this.mMap = map;

}

}

The error message I get is this:

FATAL EXCEPTION: main
                                                                         Process: com.ramaya947yahoo.mymaps, PID: 1037
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ramaya947yahoo.mymaps/com.ramaya947yahoo.mymaps.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.google.android.gms.maps.GoogleMap.getMapType()' on a null object reference
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2452)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
                                                                             at android.app.ActivityThread.access$900(ActivityThread.java:155)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:152)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5497)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.google.android.gms.maps.GoogleMap.getMapType()' on a null object reference
                                                                             at com.ramaya947yahoo.mymaps.MainActivity.onCreate(MainActivity.java:34)
                                                                             at android.app.Activity.performCreate(Activity.java:6289)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535) 
                                                                             at android.app.ActivityThread.access$900(ActivityThread.java:155) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:152) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5497) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
Zoe
  • 23,712
  • 16
  • 99
  • 132
  • I understand what a NullPointerException is, I was asking why was there still a NullPointerException after assignment. The linked post will not help other people with the same issue but thank you for the input. – Ricardo Amaya Oct 04 '17 at 14:15

1 Answers1

0

Its because you are ivoking

String mapType2 = Integer.toString(mMap.getMapType());

in onCreate method when map is not yet initializer nor assigned. All map initialization related code should be placed in

@Override
public void onMapReady(GoogleMap map) {
    this.mMap = map;
//here

}

As this is called some time after activity is already created.

Antoniossss
  • 24,977
  • 3
  • 43
  • 86
  • I understand now, thank you for the clarification! – Ricardo Amaya Oct 04 '17 at 14:17
  • I am following a Lynda.com tutorial and the original way I tried it was how they showed to do it but it seems to be outdated because they still use getMap() instead of getMapASync(). – Ricardo Amaya Oct 04 '17 at 14:51
  • it would work with getMap as it would block the call until map is ready. This is async version. Insteed of using some low quality "tutorials" why not check the google official docs about how to use map on android? – Antoniossss Oct 04 '17 at 16:16
  • Lynda.com is far from low quality and I have spent a lot of time looking at the official docs, a lot of which is difficult to follow along with if you don't have much experience in Android development. It is nothing like a YouTube tutorial if that is what you are imagining. And also, the getMap() method is depreciated, that is why it does not work anymore. – Ricardo Amaya Oct 04 '17 at 17:15
  • @RicardoAmaya your whole statement is false - official docs are more then ok. Iv started week ago my first project in Android (so 0 experience) and they are more then usefull. Saying that they are "hard to follow" for someone without experience (and by that you mean you) is so biased that should be avoided in a thinking. You can stick to Lynda.com and have more problems like that. Choice is yours. – Antoniossss Oct 04 '17 at 18:14
  • Not going to argue with you on an internet forum, but just because you have the knowledge to answer a question, that does not give you the right to be condescending and rude. Take that into consideration next time. Thanks again for the help. – Ricardo Amaya Oct 04 '17 at 20:43