sweetpoem
sweetpoem

Reputation: 33

com.google.android.gms.maps.CameraUpdate on a null object reference

this is exactly the tittle "Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.animateCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference", after press search, suddenly to main activity

logcat error

E/Response:: [{"lat":"-7.584844","lng":"110.825068","id":"11","nama":"SMK 3 NEGERI SURAKARTA"}]
09-02 20:02:09.557 16955-16955/com.skripsi.axioo.percobaan5 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.skripsi.axioo.percobaan5, PID: 16955
    java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.animateCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
        at com.skripsi.axioo.percobaan5.MapsActivity$3.onResponse(MapsActivity.java:304)
        at com.skripsi.axioo.percobaan5.MapsActivity$3.onResponse(MapsActivity.java:276)

source code

    dialog.setMessage("Loading...");
    dialog.show();

    String url = "https://arizalway.000webhostapp.com/api/lokasi.php?nama="+ambilcari;
    url = url.replaceAll(" ", "%20");
    System.out.println(url);




    StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {


        @Override
        public void onResponse(String response) {
            Log.e("Response: ", response);

            try {
                JSONArray jArray = new JSONArray(response);

                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject jsonObject = jArray.getJSONObject(i);


                    System.out.println(Arrays.toString(latLng));
                    lati = Double.parseDouble(jsonObject.getString(LAT));
                    longi = Double.parseDouble(jsonObject.getString(LNG));
                    if (ambilcari.equals("")) {


                        center = new LatLng(-7.568737, 110.827062);
                        cameraPosition = new CameraPosition.Builder().target(center).zoom(15).build();
                        gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                        getLokasi();

                    } else {
                        center = new LatLng(lati, longi);
                        cameraPosition = new CameraPosition.Builder().target(center).zoom(20).build();
                        gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


                        getLokasi();
                    }
                }

try it to search first solution but mostly the problem is not like this, this litte help for me, by adding supportfragment Google Map returning nullpointerexception Google Maps Android V2, anyone can tell me where the most critical error except from logcat, this is full code https://pastebin.com/uYPbrAun

Upvotes: 1

Views: 3993

Answers (3)

sweetpoem
sweetpoem

Reputation: 33

@faiizii awan you mean like this :

 mMap = googleMap;
        if (lati == null) {
            gMap = googleMap;
            center = new LatLng(-7.568737, 110.827062);
            cameraPosition = new CameraPosition.Builder().target(center).zoom(12).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            gMap.getUiSettings().setZoomControlsEnabled(true);
            gMap.getUiSettings().setRotateGesturesEnabled(true);
            gMap.getUiSettings().setScrollGesturesEnabled(true);
            gMap.getUiSettings().setTiltGesturesEnabled(true);


            getLokasi();
        } else {
            gMap = googleMap;
            center = new LatLng(lati, longi);
            cameraPosition = new CameraPosition.Builder().target(center).zoom(12).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            gMap.getUiSettings().setZoomControlsEnabled(true);
            gMap.getUiSettings().setRotateGesturesEnabled(true);
            gMap.getUiSettings().setScrollGesturesEnabled(true);
            gMap.getUiSettings().setTiltGesturesEnabled(true);

            getLokasi();
        }

Upvotes: 0

Faiizii Awan
Faiizii Awan

Reputation: 1710

you gMap is null. you did not initialized it. intialized it in onMapReadyMethod() as

......

public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        gMap = googleMap; //add this here
        .........
    }

Upvotes: 1

sebasira
sebasira

Reputation: 1834

In your code you have two GoogleMaps objects: gMap ang mMap. The one you are correctly getting and initializing is mMap but later on you're using gMap to place markers and update camera which don't exists and that's why you get the NPE.

Choose one of them and delete the other

Upvotes: 1

Related Questions