0

I need to save and load Json files which contains an ArrayList of Objects. Objects in the Array list : Monom Polynom and ComplexFunction.

Explanation: In this project, I have 3 main objects and 1 object that holds all of them in one list. (all of those implements "function" interface)

  1. Monom - build from two fields double coefficient, int power.
  2. Polynom - an ArrayList of Monoms
  3. ComplexFunction - holds two function objects, left and right, and operation between them (time div...)

I need to build one more object, Functions_GUI, that can contain all of those "function" in a collection... I used ArrayList called list. this object needs to have 2 main functions:

  1. saveToFile - save this Functions_GUI to a JSON with a given file name using Gson.

My Code:

public void saveToFile(String file) throws IOException {
        try 
        {
            Gson gson = new Gson();
            String json = gson.toJson(this);
            System.out.println(json);
            String file_name = file+".json";
            PrintWriter pw = new PrintWriter(new File(file_name));
            pw.write(json);
            pw.close();
        } 
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();

        }

    }

After doing so I get this file:

[
{"PloynomList":[]},
{"CF_LeftFunction":
    {"PloynomList":[
        {"Coefficient":1.0,"Power":3},
        {"Coefficient":-13.0,"Power":2},
        {"Coefficient":-2.0,"Power":0}]},
"CF_RightFunction":{
        "PloynomList":[
            {"Coefficient":1.0,"Power":1},
            {"Coefficient":-2.0,"Power":0}]},
"CF_Operation":"Divid"},
{"PloynomList":[
    {"Coefficient":1.0,"Power":3},
    {"Coefficient":-13.0,"Power":2},
    {"Coefficient":-2.0,"Power":0}]},
{"PloynomList":[
    {"Coefficient":1.0,"Power":4},
    {"Coefficient":-2.0,"Power":0}]},
{"PloynomList":[
    {"Coefficient":1.0,"Power":1},
    {"Coefficient":-2.0,"Power":0}]},
{"PloynomList":[
    {"Coefficient":12.0,"Power":1},
    {"Coefficient":-2.0,"Power":0}]},
{"PloynomList":[
    {"Coefficient":-2.0,"Power":7},
    {"Coefficient":1.0,"Power":1}]}
] 

  1. initFromFile - take a JSON file and initialize a Functions_GUI object from it.

My Code:

public void initFromFile(String file) throws IOException {
        try {
            Gson gson = new Gson();
            JsonReader reader =  new JsonReader (new FileReader(file));
            Functions_GUI tg1 = gson.fromJson(reader, Functions_GUI.class);
            this.list=tg1.list;
        }catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

The file as you can see above has been created with all the fields from all the objects.. but when I try to read the same file and create a new Function_GUI from it I get the following error:


Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: Unable to invoke no-args constructor for interface Ex1.function. Register an InstanceCreator with Gson for this type may fix this problem.
    at Ex1.Functions_GUI.initFromFile(Functions_GUI.java:125)
    at Ex1.MainTest.main(MainTest.java:183)
Caused by: java.lang.RuntimeException: Unable to invoke no-args constructor for interface Ex1.function. Register an InstanceCreator with Gson for this type may fix this problem.
    at com.google.gson.internal.ConstructorConstructor$8.construct(ConstructorConstructor.java:167)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:162)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
    at com.google.gson.Gson.fromJson(Gson.java:795)
    at Ex1.Functions_GUI.initFromFile(Functions_GUI.java:122)
    ... 1 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:48)
    at com.google.gson.internal.ConstructorConstructor$8.construct(ConstructorConstructor.java:164)
    ... 7 more
Caused by: java.lang.InstantiationException: Ex1.function
    at sun.misc.Unsafe.allocateInstance(Native Method)
    ... 13 more

Please advise!!

EladAskenazi
  • 115
  • 2
  • 10
  • Possible duplicate of [How to serialize a class with an interface?](https://stackoverflow.com/questions/4795349/how-to-serialize-a-class-with-an-interface/9550086#9550086). – LHCHIN Dec 09 '19 at 02:24
  • Could you show your `POJO` classes. It looks like `Gson` does not know which implementation to use when interface is spotted. – Michał Ziober Dec 09 '19 at 05:09

0 Answers0