[FIXED] (java.lang.Object)' on a null object reference marker.getSnippet()

Issue

I should make sure that if the incontro ID is the same as that of the marker it puts this data in the window marker like the image below. With the debug I verified that uid is Null but I don’t understand how can I use that data to be able to do that thing, does anyone have any alternatives? when i start the activity it crash and i get this error:

Database: https://imgur.com/a/hySW3Ja

The error appear in the window marker

Error:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    at com.example.chatapp.Fragments.Cercaincontro$2.getInfoContents(Cercaincontro.java:163)
    at com.google.android.gms.maps.zzf.zzb(com.google.android.gms:play-services-maps@@18.0.0:1)
    at com.google.android.gms.maps.internal.zzh.zza(com.google.android.gms:play-services-maps@@18.0.0:2)
    at com.google.android.gms.internal.maps.zzb.onTransact(com.google.android.gms:play-services-maps@@18.0.0:3)
    at android.os.Binder.transact(Binder.java:1043)
    at em.bb(:com.google.android.gms.dynamite_mapsdynamite@222615084@22.26.15 (150700-0):2)
    at com.google.maps.api.android.lib6.impl.bx.a(:com.google.android.gms.dynamite_mapsdynamite@222615084@22.26.15 (150700-0):7)
    at com.google.maps.api.android.lib6.gmm6.api.f.h(:com.google.android.gms.dynamite_mapsdynamite@222615084@22.26.15 (150700-0):4)
    at com.google.maps.api.android.lib6.gmm6.api.g.h(:com.google.android.gms.dynamite_mapsdynamite@222615084@22.26.15 (150700-0):0)
    at com.google.maps.api.android.lib6.impl.cz.l(:com.google.android.gms.dynamite_mapsdynamite@222615084@22.26.15 (150700-0):2)
    at com.google.maps.api.android.lib6.impl.cz.f(:com.google.android.gms.dynamite_mapsdynamite@222615084@22.26.15 (150700-0):7)
    at com.google.maps.api.android.lib6.gmm6.api.f.j(:com.google.android.gms.dynamite_mapsdynamite@222615084@22.26.15 (150700-0):2)
    at com.google.maps.api.android.lib6.gmm6.vector.l.a(:com.google.android.gms.dynamite_mapsdynamite@222615084@22.26.15 (150700-0):17)
    at com.google.maps.api.android.lib6.gmm6.vector.ae.U(:com.google.android.gms.dynamite_mapsdynamite@222615084@22.26.15 (150700-0):15)
    at com.google.maps.api.android.lib6.gmm6.vector.cp.onSingleTapConfirmed(:com.google.android.gms.dynamite_mapsdynamite@222615084@22.26.15 (150700-0):0)
    at com.google.maps.api.android.lib6.impl.gesture.b.onSingleTapConfirmed(:com.google.android.gms.dynamite_mapsdynamite@222615084@22.26.15 (150700-0):0)
    at com.google.maps.api.android.lib6.impl.gesture.c.handleMessage(:com.google.android.gms.dynamite_mapsdynamite@222615084@22.26.15 (150700-0):2)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)



public class Cercaincontro extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener {

    private FragmentsCercaincontroBinding binding;
    LatLng latLng;
    private ProgressDialog progressDialog;
    private DatabaseReference reference,reference1,reference2;
    private FirebaseUser firebaseUser;
    private View windowView;
    private List<Incontro> Incontri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragments_cercaincontro);


        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        reference = FirebaseDatabase.getInstance().getReference().child("Incontro");
        Incontri = new ArrayList<>();


         ValueEventListener eventListener = new ValueEventListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshotincontro) {
                for (DataSnapshot b : snapshotincontro.getChildren()) {
                    Incontro incontro = b.getValue(Incontro.class);
                    Incontri.add(incontro);
                }


                    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapincontro);
                    assert mapFragment != null;
                    mapFragment.getMapAsync(Cercaincontro.this);


            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                throw error.toException();
            }
        };

        reference.addListenerForSingleValueEvent(eventListener);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {


     try{
            for (int i = 0; i < Incontri.size(); i++) {
                String incontro_address = Incontri.get(i).getAddressorganizz();
                if (incontro_address == null) continue;

                latLng = getLocationFromAddress(incontro_address);
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(latLng);
                markerOptions.snippet(Incontri.get(i).getIncontroidId());
                MarkerOptions Gruppo = markerOptions.icon(BitmapFromVector(getApplicationContext(), R.drawable.ic_group_primary));

                    googleMap.addMarker(markerOptions);
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        googleMap.setOnInfoWindowClickListener(this);


        googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Nullable
            @Override
            public View getInfoContents(@NonNull Marker marker) {

                windowView = View.inflate(getApplicationContext(), R.layout.window_group_map, null);


                TextView cittaorg = (TextView) windowView.findViewById(R.id.Citywindow);
                TextView data = (TextView) windowView.findViewById(R.id.Datawindow);
                TextView orario = (TextView) windowView.findViewById(R.id.Orarioincontrowindow);
                TextView partecipanti = (TextView) windowView.findViewById(R.id.Partecipantiwindow);
                TextView summary = (TextView) windowView.findViewById(R.id.Summarywindow);

            

                String uid = marker.getSnippet();
                for(Incontro incontro : Incontri) {
                    if(incontro.getIncontroidId().equals(uid)) { //error line
                        cittaorg.setText("Città: " + incontro.getCittaorg());
                        orario.setText("Ora: " + incontro.getOrario());
                        data.setText("Data: " + incontro.getDate());
                        partecipanti.setText("Partecipanti: " + incontro.getPartecipanti());
                        summary.setText("Descrizione: " + incontro.getSummary());
                        break;
                    }
                }

                    return windowView;
            }

            @Nullable
            @Override
            public View getInfoWindow(@NonNull Marker marker) {
                return null;
            }
        });
    }

        public LatLng getLocationFromAddress(String strAddress) {

            Geocoder coder = new Geocoder(this);
            List<Address> addressIncontro;
            LatLng p1;

            try {
                addressIncontro = coder.getFromLocationName(strAddress, 5);
                if (addressIncontro == null) {
                    return null;
                }
                Address location = addressIncontro.get(0);
                p1 = new LatLng(location.getLatitude(), location.getLongitude());

                return p1;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

Solution

If you want to get the child. You can do like this. I think you need to readjust it.

for (DataSnapshot b : snapshotincontro.getChildren()) {
    Incontro incontro = b.getValue(Incontro.class);
    //At here you need to add the getIncontroidId
    final String key = b.getKey();
    incontro.setIncontroidId(key);
    Incontri.add(incontro);
}

One more thing, I would like to prefer you to call ValueEventListener inside the onMapReady() because, something calling ValueEventListener maybe a bit delay and make the list become empty.

Answered By – Ticherhaz FreePalestine

Answer Checked By – Laura B. (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published