I'm new in coding. I want to add a myObjectChild to a static list that is declared in the class of MyObjectChild. I get an error when trying to add to the static list, but it works fine to add the same object to a list declared in the method. I don't know what's wrong. I want to add the object to the static list. I created the list in the method only to test if the problem was with the object or with the adding to the static list and it seems that the problem is when adding to the static list. Can someone help me?? Here is my code:
public class MyObjectChild extends MyObject {
int id;
String name;
static List <MyObject> staticList = new ArrayList<>();
static String jsonStaticList = null;
@Override
public void addToList () {
List<MyObject> list = new ArrayList<>();
try {
list.add(this); // this works when running the app
} catch (Exception e){
Log.e("error", "Didn’t add MyObject to the methodList "); }
try {
staticList.add(this); // this doesn't work when running the app
} catch (Exception e){
e.printStackTrace(); } //got this message: E/ViewRootImpl﹕ sendUserActionEvent() mView == null
}