java - Adding objects to an ArrayList from another class -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
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 innerclass
but 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
Post a Comment