0

I have a json structure that looks something like this

{"overallobject
    "object1": {
    },
    "object2": {
        "field1": null,
        "listObject": [{
             "field2": null,
             "field3": null,     
        }]
    }
}

My model class of this json object looks like this:

public class Overallobject {

    public static class Object1{
    }

    public static class Object2{
        private String field1;
        public List<ListObject> listObject = new ArrayList<ListObject>();
    }

    public static class ListObject{

        public String field2;
        public String field3;

    }
}

I have a model class that turns the above json into POJO and a service class that turns the model class into a string with some additions.

In my service class, I can execute the code perfectly fine like this:

import Overallobject;

public class Service() {
    Overallobject overallobject = new Overallobject();
    List<ListObject> listObject = new ArrayList<ListObject>;
    overallobject.getList.add(listObject);      // this lets me add objects to the list array so its not empty when instantiated 
}

However I was wondering if I could instantiate the list objects within the model class instead of the service class. I've tried to create the list object inside the model class but I can't seem to add it and I get the error. If i don't try to instantiate the list object, listObject returns an empty array [].

class member declared outside of a class java
mclovin56
  • 3
  • 2
  • 2
    The code you have presented is full of syntactical errors and should even compile. Please try to share the full/correct code in order for someone to help you. – akortex Aug 25 '21 at 06:39
  • @akortex sorry about that, i've edited it to clean it up – mclovin56 Aug 25 '21 at 06:51
  • Note that `List listObject = new List()` is a **raw** type, as you completely leave out the generic type declarations here. Fix that part first, it is essential. See https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – GhostCat Aug 25 '21 at 06:54
  • 1
    We still dont have a [mcve] though. Dont **explain** to us what code you wrote that gave you that error: **show** us the code that gives the error. – GhostCat Aug 25 '21 at 07:35

0 Answers0