gpt4 book ai didi

使用时出现 Java ClassCastException?延伸

转载 作者:行者123 更新时间:2023-11-30 10:10:57 28 4
gpt4 key购买 nike

我有一个获取这种类型参数的方法:

NSArray<? extends FIRQueryDocumentSnapshot>

现在,当我获取此数组的一个元素时,我得到一个 ClassCastException。我的方法:

public static List<AbstractWrapper> getWrapperList(Class<? extends IAbstractWrapper> wrapper, NSArray<? extends FIRQueryDocumentSnapshot> documentSnapshotArray){
List<AbstractWrapper> results = new ArrayList();
for(int i=0; i<documentSnapshotArray.size(); i++){
FIRQueryDocumentSnapshot doc = documentSnapshotArray.get(i);
results.add(getWrapper(wrapper,doc));
}
return results;
}

第4行错误

java.lang.ClassCastException: apple.NSObject cannot be cast to org.moe.binding.firebasefirestore.FIRQueryDocumentSnapshot

最佳答案

这可能是因为您从 documentSnapshotArray.get(i) 获取的对象无法转换为 FIRQueryDocumentSnapshot,请通过以下方式验证

System.out.println(documentSnapshotArray.get(i) instanceof FIRQueryDocumentSnapshot);  
// if true problem is something else but,
// if false then object you are getting is not a type of FIRQueryDocumentSnapshot

解决此问题的方法是:-

  • 如果你得到的对象不是FIRQueryDocumentSnapshot,那么你应该修复代码生成 documentSnapshotArray 并确保您放置的每个元素在 documentSnapshotArray 中应该是 (IS-A)FIRQueryDocumentSnapshot。

关于使用时出现 Java ClassCastException?延伸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52662801/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com