having a problem trying to get an arraylist containing arraylists to work in my program, Im getting a strange warning saying: add @SuppressWarnings "null" to processArray(), regardless of If I add this or not my program crashes, Is there anything obvious that Im doing wrong here? any help would go a long way thanks.
private ArrayList<ArrayList<String>> processArray(ResponseList<Status> responses){
ArrayList<ArrayList<String>> mainArray = null;
ArrayList<String> innerArrays = null;
for (Status response: responses ){
String name, status, imgUrl, time;
name = response.getUser().getName();
status = response.getText();
imgUrl = response.getUser().getProfileImageURL().toString();
time = response.getCreatedAt().toString();
ArrayList<String> rtLinks = checkLinks(response.getText());
if(rtLinks != null){
for (String tLink: rtLinks){
innerArrays.add(name);
innerArrays.add(status);
innerArrays.add(imgUrl);
innerArrays.add(time);
innerArrays.add(tLink);
mainArray.add(innerArrays);
}
}
}
return mainArray;
You never actually initialize either of those arraylists.
You want
ArrayList<ArrayList<String>> mainArray = new ArrayList<ArrayList<String>>(); // or new ArrayList<>() in java 7
and inside the inner for loop:
ArrayList<String> innerArrays = new ArrayList<String>(); // or new ArrayList<>() in java 7
Also, don't ever suppress warnings "just to make code work". Only suppress them when you know exactly why they appear and exactly why you're choosing to ignore them.
Your program is probably crashing due to a NullPointerException. This occurs when you try to access a variable whose value is null (known as dereferencing a null pointer). Before you access either of the two ArrayLists, you'll need to initialize it. Try changing the two lines to this:
ArrayList<ArrayList<String>> mainArray = new ArrayList<ArrayList<String>>();
ArrayList<String> innerArrays = new ArrayList<String>();
This should solve your immediate problem.
However, I'm not sure why you're using an innerArrays list at all. I would refactor the last bit to this:
for (String tLink: rtLinks){
ArrayList<String> tempList = new ArrayList<String>();
tempList.add(name);
tempList.add(status);
tempList.add(imgUrl);
tempList.add(time);
tempList.add(tLink);
mainArray.add(tempList);
}
and remove the innerArrays variable. This way, the scope is more limited, and the code operates more logically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With