java - Adding objects to an ArrayList from another class -


this question has answer here:

i trying add objects arraylist file getting java.lang.nullpointerexception though have initialized arraylist. here example of code structure talking about.

//objectone.java  import java.util.arraylist;  public class objectone {         public innerclass inner;     class innerclass {         arraylist<string> list = new arraylist<string>();         public void addstring(string str) {            list.add(str);        }     }     public void addstr(string str) {        inner.list.add(str);    }   } 

here second file:

//objecttwo.java public class objecttwo {      public static void main(string args[]) {         objectone obj = new objectone();         obj.addstr("test string added");     }  } 

i have initialized arraylist in innerclassbut when try add item in java file nullpointerexception. reason have file structure because working gson. why arraylist list acting if never initialized? goal able add objects list different java file.

you did not create instance of inner class in parent class, created reference it.

public innerclass inner = new innerclass(); // add new here 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -