Wednesday 29 March 2017

ArrayList can not add ArrayList, error java.lang.NullPointerException

Your first statement


ArrayList[] arraylist=new ArrayList[2];

allocates an array object that can reference two ArrayList(s). It doesn't instantiate those ArrayList(s). And raw-types are a bad idea. But you could add something like,


ArrayList[] arraylist = new ArrayList[2];
arraylist[0] = new ArrayList();
arraylist[1] = new ArrayList();

And I get


[Ngyen, Van, Jone]
[20, 40, 28]

But the above has no type safety. As you added String and Integer instances to the two List(s).

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...