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

3

5

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)

Oujda answered 29/12, 2015 at 7:18 Comment(3)
What is your question?Nusku
Plz include the code where you are initializing GoogleMap object .Folliculin
Please check the code.Oujda
I
9

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

Isocracy answered 29/12, 2015 at 7:52 Comment(3)
Have you removed the line from onCreate() method??? I have edited my solution please read it and remove the line i have mentioned.Isocracy
Could please accept the answer so your question wont remain unanswered.Isocracy
this helped me alot.... Bundles of Thanks ❤😊😊Cannibal
P
2

First, we need the code where the bug happened. I mean you just put your logcat ouput, not formatted, which is not enough.

Then your logcat said NullPointerException. It means that you are trying to set a listener on a object with a null value.

So all we can do to help you with what you provided is to check that the object for which you set the setOnMapLongClickListener is not null before calling it.

EDIT

Ok with your code it seems that the map might have not been initialized, for some reasons. What you can do is checking if the map is available or not before setting the listener to it. Otherwise you will try to set your listener to a null object which will lead to a crash.

Before this line:

 mMap.setOnMapLongClickListener(this);

Ensure that your mMap has been initialized. Because your method setting the value to your mMap object is not called before you set the listener.

Perspiratory answered 29/12, 2015 at 7:23 Comment(0)
S
2

As far as I can see, you need to initialize the map before interacting with it.

The onCreate Method gets called when the activity gets created. The onMapReady not. So please initialize the map fist and then try to register an event listener.

Succubus answered 29/12, 2015 at 7:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.