Class java.util.Map has generic type parameters, please use GenericTypeIndicator instead

Sahaj Rana picture Sahaj Rana · Jun 7, 2016 · Viewed 17.8k times · Source

I am using firebase to retrieve data from database n use

Map<String, String> map = dataSnapshot.getValue(Map.class);

to get values, but it shows error

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: com.rana.sahaj.myyu, PID: 13179
                                                                 com.google.firebase.database.DatabaseException: Class java.util.Map has generic type parameters, please use GenericTypeIndicator instead
                                                                     at com.google.android.gms.internal.zzaix.zzb(Unknown Source)
                                                                     at com.google.android.gms.internal.zzaix.zza(Unknown Source)
                                                                     at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                     at com.rana.sahaj.myyu.profile.Profile$2.onDataChange(Profile.java:158)
                                                                     at com.google.android.gms.internal.zzafp.zza(Unknown Source)
                                                                     at com.google.android.gms.internal.zzagp.zzSu(Unknown Source)
                                                                     at com.google.android.gms.internal.zzags$1.run(Unknown Source)
                                                                     at android.os.Handler.handleCallback(Handler.java:733)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:136)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5052)
                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                     at dalvik.system.NativeStart.main(Native Method)

here's the code

 DatabaseReference profileRef=mFirebaseRef.child(EEmail);
    profileRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

    -->     Map<String, String> map = (Map<String,String>)dataSnapshot.getValue(Map.class);

            name = (TextView)findViewById(R.id.UserInActivity);
            EmailView= (TextView)findViewById(R.id.emailUser);

            PhotoUrl = map.get("picurl");
             emmaill=map.get("userEmail");

            UserNam = map.get("userNAME");

            name.setText(UserNam);
            EmailView.setText(emmaill);


        }

        @Override
        public void onCancelled(DatabaseError firebaseError) {

        }
    });

n there is no problem with key n values in database. used the solution but not working

Answer

Shubhank picture Shubhank · Jun 7, 2016

The error points out correctly where you are going wrong

 Map<String, String> map = dataSnapshot.getValue(Map.class);

Map class uses parameter to define the types of Key and Object where as you don't give them and simply use Map.class which fails.

Try the below code - since Key are always string and we can have any type of Object for them

    Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();