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.
You are trying to add the innerArrays variable into your main ArrayList, but you have declared your main ArrayList null in the beginning of the method. Initialize the main ArrayList as a new ArrayList of ArrayLists of Strings, and your code should work.
Related
I am trying to use remove method but I am getting messed up as I keep getting error for this:
List<String> list1 = new ArrayList<String>();
list1.add("Apple");
list1.add("Bus");
list1.add("Cat");
list1.add("Dog");
String ne = list1.listIterator().next();
System.out.println(ne);
list1.listIterator().remove();
the remove statement throws an error?? can anyone tell me why??
same thing when I just use iterator in place of listIterator?
Your code is correct in that you are allowed to call ListIterator#remove() once for each time next() is called (see Javadoc). However, in your code you are actually calling remove() on a new list iterator, not the one you used before it. Try the following code and hopefully it will be clear what you did wrong:
List<String> list1 = new ArrayList<String>();
list1.add("Apple");
list1.add("Bus");
list1.add("Cat");
list1.add("Dog");
// retain a reference to the same list iterator
ListIterator<String> list1itr = list1.listIterator();
String ne = list1itr.next();
System.out.println(ne);
list1itr.remove();
Demo
I have a method that needs to return List<'MyClass>[] and need to set up a local variable to do so, but am having trouble with initialization.
I tried:
List<MyClass>[] lists = new List<MyClass>[5];
Which gave me an error of "Cannot create a generic array of List"
I tried casting an array of Objects:
List<MyClass>[] lists = (List<MyClass>[]) new Object[5];
Which gave me a casting error in runtime.
I also tried:
List<MyClass>[] lists = (List<MyClass>[]) new List[5];
Which resulted in a null pointer exception.
Anyone know what needs to be done to get this to work?
Thanks.
You declare a list like so (for example ArrayList):
List<MyClass> list = new ArrayList<MyClass>();
To create an array of this list you do:
List<?>[] listArray = new List<?>[]{list};
This will put your list in an array. I'm assuming that's what you want and not simply items from the list.
I'm trying to use the ArrayList() method in Processing.
I have this:
ArrayList trackPoints = new ArrayList();
//inside a loop
int[] singlePoint = new int[3];
singlePoint[0] = 5239;
singlePoint[1] = 42314;
singlePoint[2] = 1343;
//inside a loop
trackPoints.add(singlePoint);
So basically I want to add an array "singlePoint" with three values to my ArrayList.
This seems to work fine, because now I can use println(trackPoints.get(5)); and I get this:
[0] = 5239;
[1] = 42314;
[2] = 1343;
However how can I get a single value of this array?
println(trackPoints.get(5)[0]); doesn't work.
I get the following error:
"The type of the expression must be an array type but it resolved to Object"
Any idea what I'm doing wrong? How can I get single values from this arrayList with multiple arrays in it?
Thank you for your help!
Your ArrayList should by typed :
List<int[]> list = new ArrayList<int[]>();
If it's not, then you're using a raw List, which can contain anything. Its get method thus returns Object (which is the root class of all the Java objects), and you must use a cast:
int[] point = (int[]) trackPoints.get(5);
println(point[0]);
You should read about generics, and read the api doc of ArrayList.
The get() method on ArrayList class returns an Object, unless you use it with generics. So basically when you say trackPoints.get(5), what it returns is an Object.
It's same as,
Object obj = list.get(5);
So you can't call obj[0].
To do that, you need to type case it first, like this:
( (int[]) trackPoints.get(5) )[0]
I am using an arrayList in one of my code. I am finding it difficult to retrieve elements out of an arraylist. it gives me an error when i use this:
getit = newlist.get(g);
Basically I want the string variable getit to store the value of the gth element of the arraylist called 'newlist' and then use it.
Could someone help with this? I am using Processing language for this.
All help will be appreciated,
Thanks in advance,
Amrita
maybe
getit = newlist.get(g-1);
lists start with 0. so if You are getting java.lang.IndexOutOfBoundsException that might be the problem.
ArrayList newList = new ArrayList();
newList.add("One");
newList.add("Two");
System.out.println( newList.get(1) ); // retrieve second el.
System.out.println( newList.size() ); // get number of elements
works fine for me.
Please note, that lists in Java (and therefore in Processing) are 0 based. That means, the first index is 0.
Either newList is not an ArrayList
Or g is not an int
Or getit is not a String
This is assuming error is a compile time error and not a run time error.
To get the length myList.size();
You should check the javadocs before posting questions:
ArrayList
So according to your post you are getting cannot convert from Object to String. This is because you are using an Object ArrayList instead of a String ArrayList. Use the following...
ArrayList<String> myList = new ArrayList<String>();
myList.add("value");
int g = 0;
String val = myList.get(g);
I know the title sound confusing and thats because it is. its a bit long so try too stay with me.
this is the layout i have my code designed
variables
constructor
methods.
im trying too fill a Jlist full on names. i want too get those names using a method. so here goes.
in my variables i have my JList. its called contactNames;
i also have an array which stores 5 strings which are the contacts names;
heres the code for that anyway
String contact1;
String contact2;
String contact3;
String contact4;
String contact5;
String[] contactListNames;
JList contactList;
simple enough. then in my constructor i have the Jlist defined to fill itself with the contents of the array
String[] contactListNames = new String[5];
JList contactList = new JList(contactListNames);
fillContactList();
that method fillContactList() is coming up shortly.
now heres where stuff gets balls up.
ive created three different methods all of which havent worked. basically im trying to fill the array with all of them.
this is the simplest one. it doesnt set the Jlist, it doesnt do anything compilicated. all it trys too do is fill the array one bit at a time
public void fillContactList()
{
for(int i = 0;i<3;i++)
{
try
{
String contact;
System.out.println(" please fill the list at index "+ i);
Scanner in = new Scanner(System.in);
contact = in.next();
contactListNames[i] = contact;
in.nextLine();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
unfortunately this doesnt qwork. i get the print out to fill it at index 0; i input something and i get a nice big stack trace starting at
contactListNames[i] = contact;
so my question in short is
why cant i fill the array from that method.
***********************************************888 ***********************************************888
stack trace by request
please fill the list at index 0
overtone
please fill the list at index 1
java.lang.NullPointerException
at project.AdminMessages.fillContactList(AdminMessages.java:410)
at project.AdminMessages.<init>(AdminMessages.java:91)
at project.AdminUser.createAdminMessages(AdminUser.java:32)
at project.AdminUser.<init>(AdminUser.java:18)
at project.AdminUser.main(AdminUser.java:47)
To define an array in a constructor you can do something along these lines,
// if values are predefined, you can explicitly fill the array
String[] contacts = {"Bill Gates", "Steve Jobs", "Jon Skeet"};
// or this way, both will work.
String[] contacts = new String[2];
Looking at JList from the Java Doc's you can most certainly pass in an array to JList
String[] data = {"one", "two", "three", "four"};
JList dataList = new JList(data);
You are getting NullPointerException because the array, contactListNames is not initialized, you would need to initialize it.
You define an array in a constructor just like you would any other variable. So, it would look something like:
// define an array of size 3
String[] contactListNames = new String[3];
The reason you are getting exceptions is because you don't actually initialize the array. You declare it but you never set it to a value (or give it a size). You should post the stack trace of the error but I suspect it's a NullPointerException.
Then in my constructor i have the
Jlist defined to fill itself with the
contents of the array
String[] contactListNames = new String[5];
JList contactList = new JList(contactListNames);
fillContactList();
What you're doing here is creating new local variables that are shadowing the ones defined in your class.
Change it to:
contactListNames = new String[5];
contactList = new JList(contactListNames);
fillContactList();