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.
4 Answers 4
The problem is you are doing assignments on method calls. You are calling the method two times. so the second line creates another array list
loopthrougharrays()[1] = new ArrayList();
loopthrougharrays()[1].add(s) //This one will call the method again
// and get new array list and the previous value is lost
Simple fix
Object[] getTwoArrays = loopthrougharrays()
ArrayList L0 = <ArrayList> getTwoArrays[0];
ArrayList L1 = <ArrayList> getTwoArrays[1];
L1 = new ArrayList();
L1.add(s);
Here the loopthrougharrays() is called once and the return value is stored into a local reference and then it works
While I try to provide a quick fix provided, i would not return null arraylits in a object array and then initialize outside the method and do assignments. it's clumsy.
A lesser evil way is
class SomeX {
private List Ll = new ArrayList();
private List L2 = new ArrayList();
public addToL1(Object s) {
L1.add(s);
}
public addToL2(Object s) {
L2.add(s)
}
}
//main method
SomeX x = new SomeX();
x.addToL1(s);
x.addToL2(s);
Comments
I don't know what do you want to do with that, but it is not a good practice. However it could work, if you cast your Object array element to an ArrayList like:
((ArrayList)t.loopthrougharrays()[1]).add(s);
And delete loopthrougharrays()[1] = new ArrayList(); (like @Tom comment says, thanks) or replace it with
ArrayList myNewArrayList = (ArrayList)loopthrougharrays()[1];
myNewArrayList.add(s);
Comments
Cast it before you add anything to your list.
((ArrayList<YourVariableType>)(loopthrougharrays()[1])).add(s);
But remember, this is not a good practice. I'm pretty sure, you can achieve what you want to achieve with a better design and with best practices.
Comments
Your tables is an array of objects ,
tables[1] = listB15; // tables still remains as array of object and listB15 is considered as an object since Object is the Super class .
To achieve wt u want you can try these methods :
((List<String>) tables[1]).add("hello world"); // cast it with List<String> (for example)create a list
List<List<String>> tables = new ArrayList<List<String>>(); tables1.add(list) ; tables1.add(listB15) ; tables1.get(1).add("hello world"); System.out.println(tables);
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[]).