我在我的項目做出一個JSON文件中的一些巨大變化,現在GSON與此錯誤抱怨保護java.util.AbstractCollection中的()源文件可以be found here。GSON錯誤:</p> <pre><code>java.lang.RuntimeException: Failed to invoke protected java.util.AbstractCollection() with no args </code></pre> <p>的JSON:無法調用與無參數
我對相應的對象實現可以be found here
我發射了GSON這樣的:
new Gson().fromJson(json.toString(), Config.class);
,集成GSON可以be found here
我做了一些關於清理周圍的源文件所以我發現了這篇文章,但鏈接被破壞了,所以我無法真正掌握這個問題。 https://stackoverflow.com/questions/17351720/deserializing-a-custom-object-with-gson-error-failed-to-invoke-public-custom-cla
我覺得可以是導致該問題的唯一事情是在Config.java:91
private AbstractCollection<String> expectedMessages = null;
public Structure() {
if(this.messagesInOrder) {
this.expectedMessages = new ArrayList<>();
} else {
this.expectedMessages = new HashSet<>();
}
}
發現這個代碼塊,但我不知道爲什麼它會導致錯誤,因爲你可以這樣做一般。也許Gson不構造內部類的新實例?
下面是引用
Exception in thread "main" java.lang.RuntimeException: Failed to invoke protected java.util.AbstractCollection() with no args
at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:107)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:78)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.google.gson.Gson.fromJson(Gson.java:689)
at config.ParseConfig.convertJsonToObject(ParseConfig.java:70)
at config.ParseConfig.parseConfig(ParseConfig.java:36)
at JavaGrader.main(JavaGrader.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:104)
... 18 more
Process finished with exit code 1