How to get the value of a class with generic type in property?

前提是你 提交于 2019-12-22 10:53:22

问题


I have a class Party

public class Party {
    private String password;
    private String host;
    private String nextInLine;
    private String nextUp;
    private Track currentlyPlaying;
    private LinkedList<Guest> guestList;

    public Party() {
        // Default constructor required for calls to DataSnapshot.getValue(Post.class)
    }

    public Party(String password, String hostName) {
        this.password= password;
        this.host= hostName;

        LinkedList<Guest> newGuestList= new LinkedList<>();
        newGuestList.put(hostName, new Guest(hostName));
        this.guestList= newGuestList;
    }

    public Party(String password, String host, String nextInLine, String nextUp, Track
        currentlyPlaying, LinkedList<Guest> guestList) {
        this.password = password;
        this.host = host;
        this.nextInLine = nextInLine;
        this.nextUp = nextUp;
        this.currentlyPlaying= currentlyPlaying;
        this.guestList= guestList;
    }

    ...
}

I am trying to retrieve information from the database with the line

Party party = mutableData.getValue(Party.class);

This produces ArrayIndexOutOfBounds exception. However, if I comment out the

private LinkedList<Guest> guestList;

line, the error is avoided. My LinkedList class is

public class LinkedList<Type> extends HashMap<String, Type> {...}

with default constructors, and my guest class is

public class Guest extends Node {
    private String id;
    private String next;
    private String previous;
    private LinkedList<Track> playlist;

    public Guest(){}

    public Guest(String guestName) {
        super(guestName);
        this.id= guestName;
    }

    ...

}

I've tried other methods of extracting the data, such as

GenericTypeIndicator<Party> partyType = new
                    GenericTypeIndicator<Party>() {};
Party party = mutableData.getValue(partyType);

and

Party party= (Party) mutableData.getValue();

to no avail. I believe it has to do with LinkedList class having a generic type, but the GenericTypeIndicator only solves the problem if it is applied directly to the generic type, rather than the generic type being in a property of the class. Does anyone know what might be going on?

EDIT: The stacktrace is

postTransaction:onComplete:DatabaseError: User code called from the Firebase Database runloop threw an exception:
                                                              java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
                                                                  at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbqi$zza.zze(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbqi$zza.zzaG(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbqi.zze(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
                                                                  at com.google.firebase.database.MutableData.getValue(Unknown Source)
                                                                  at com.app1x.djparty.MainActivity$4.doTransaction(MainActivity.java:293)
                                                                  at com.google.android.gms.internal.zzbml.zzb(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbml.zzo(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbml.zzb(Unknown Source)
                                                                  at com.google.android.gms.internal.zzbml$8.zzan(Unknown Source)
                                                                  at com.google.firebase.database.connection.idl.zze$2.zzan(Unknown Source)
                                                                  at com.google.firebase.database.connection.idl.zzm$zza.onTransact(Unknown Source)
                                                                  at android.os.Binder.transact(Binder.java:499)
                                                                  at bqq.a(:com.google.android.gms.DynamiteModulesC:82)
                                                                  at bqh.a(:com.google.android.gms.DynamiteModulesC:228)
                                                                  at bop.a(:com.google.android.gms.DynamiteModulesC:966)
                                                                  at bok.a(:com.google.android.gms.DynamiteModulesC:378)
                                                                  at bny.a(:com.google.android.gms.DynamiteModulesC:1139)
                                                                  at bpb.b(:com.google.android.gms.DynamiteModulesC:225)
                                                                  at bpb.a(:com.google.android.gms.DynamiteModulesC:270)
                                                                  at bpi.run(:com.google.android.gms.DynamiteModulesC:1021)
                                                                  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
                                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                  at java.lang.Thread.run(Thread.java:761)

来源:https://stackoverflow.com/questions/41948309/how-to-get-the-value-of-a-class-with-generic-type-in-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!