androidandroid-maps

Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setOnMapLongClickListener' on a null object reference


When i long press the map it should add marker into the place. But its not working. My app crashes when i m trying to run this. Please help me out

Here's my code

package inandroid.jeetna.memorableplaces;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback,GoogleMap.OnMapLongClickListener {

    private GoogleMap mMap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        mMap.setOnMapLongClickListener(this);

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);


        Intent i = getIntent();
        Log.i("locationInfo", Integer.toString(i.getIntExtra("locationinfo", -1)));
    }



    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    @Override
    public void onMapLongClick(LatLng point) {
        mMap.addMarker(new MarkerOptions()
                .position(point)
                .title("You are here")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    }

}

This is error message i m getting

12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: FATAL EXCEPTION: main 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: Process: inandroid.jeetna.memorableplaces, PID: 7519 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{inandroid.jeetna.memorableplaces/inandroid.jeetna.memorableplaces.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setOnMapLongClickListener(com.google.android.gms.maps.GoogleMap$OnMapLongClickListener)' on a null object reference 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:144) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5221) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setOnMapLongClickListener(com.google.android.gms.maps.GoogleMap$OnMapLongClickListener)' on a null object reference 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at inandroid.jeetna.memorableplaces.MapsActivity.onCreate(MapsActivity.java:33) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5937) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)  12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:144)  12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)  12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)  12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)  12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5221)  12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)  12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)  12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)  12-29 12:42:07.118 7519-7519/inandroid.jeetna.memorableplaces E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


Solution

  • In your Long click method do something like below code

    public void onMapLongClick(LatLng point) {
           if(mMap != null){
            mMap.addMarker(new MarkerOptions()
                    .position(point)
                    .title("You are here")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
          }
     }
    

    And move the line mMap.setOnMapLongClickListener(this); from onCreate() method to onMapReady() method as follows:

        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
            mMap.setOnMapLongClickListener(this);
    
            //your code
        }
    

    This is because your mMap is null until it assigned the googleMap object from onMapReady() method