I have 2 arraylists situated inside an array as follows:
public Object[] loopthrougharrays() {
Object[] tables = new Object[2];
tables[0] = list;
tables[1] = listB15;
return tables;
}
My 2 arraylists are called list and listB15.
I can then call my arraylists from another method like
loopthrougharrays()[1] = new ArrayList();
which is listB15.
However if I try to add an item to the ArrayList like
loopthrougharrays()[1].add(s)
where s is a variable
Java doesn't recognize the loopthrougharrays()[1] as an ArrayList.
How can I add the variable via this method?
I appear to get the following error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
My entire code works fine if I do the following:
listB15 = new ArrayList();
listB15.add(s)
as I would expect.
Object, how should the compiler know what instances it contains? And btw: you get the NPE becauseloopthrougharrays()creates a new array and that doesn't care about yourloopthrougharrays()[1] = new ArrayList();statement.tablesanArrayList[](or even better aList[]).