Reputation: 1
I have a class titled Globals
and I'm trying to add elements while using a different class, but I keep getting the java.util.NoSuchElementException
error. Here is my code:
ListIterator lit=listOfNames.
while(lit.hasNext())
{
Globals.listOfNames.add(lit.next().toString());
Log.e(TAG, lit.next()+" ");
}
and I initialized it in the global class as:
public static ArrayList<String> ListOfStdntNames=new ArrayList<>();
Why is the exception being thrown? Thank you for the help.
Upvotes: 0
Views: 72
Reputation: 119
First, you should not define your arraylist as static.
you should do it like this:
add a method inside global class:
public void AddToArrayList(String yourValue){
yourArrayList.add(yourValue);
}
then call that method from other class:
Global global = new Global();
while(list.hasNext()) {
String nextVal = list.next().toString();
global.AddToArrayList(nextVal);
}
Upvotes: 0
Reputation: 1944
You are calling the next()
function twice inside your loop. Once during add and once during Log.
Change it like this:
while(list.hasNext()) {
String nextVal = list.next().toString();
Globals.listOfNames.add(nextVal);
Log.e(TAG, nextVal+" ");
}
Upvotes: 2