Well, this is somewhat stupid question but I want to explore something new that's why posting this question. Please! before marking it duplicate or irrelevant post your answer.
Q: How to use Array of ArrayList specially when the ArrayList is in other class and Array of ArrayList in main()?
class ArrayListDemo {
private ArrayList<Type> arrList = new ArrayList<Type>();
public void addItem(Type x) {
arrList.add(x);
System.out.println("Type Added: " + x);
}
}
Now, main() is like:
public void main(String[] args) {
ArrayListDemo[] arr = new ArrayListDemo[10];
Type x = somethingSilly;
arr[0].addItem(x); // <-- java.lang.NullPointerException
}
What I'm missing or what's wrong with this? I know, there are better options like available List<> etc but I'm given a task to do it just via Array of ArrayList.