This question already has answers here:
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
(31 answers)
Closed 7 years ago.
my Java (libgdx) application/game throws me java.util.ConcurrentModificationException.
My code:
for (Lod lod : lode) {
if (!pause){
lod.move();
}
if (lod.isDestroyed()){
lode.remove(lod);
} else {
lod.draw(game.batch);
}
}
You try to remove an element while you are iterating over the collection. This is simply not possible by the iterator.
Related
This question already has answers here:
How to create a Java 8 Stream from an iterator?
(4 answers)
Closed 1 year ago.
I'm trying to iterate through the elements in a JsonNode and check if they match some condition. I want to use Streams instead of using the classic iterator. I have code similar to this:
return Stream.generate(jsonNode.fields()::next)
.allMatch(entry -> {
switch (entry.getKey()) {
case "a":
return evaluateA(entry.getValue());
case "b":
return evaluateB(entry.getValue());
default:
return false;
}
});
But when I run this, I get a java.util.NoSuchElementException. I'm guessing the return statements within the switch case are causing this error. If that is the case, how do I handle this?
You can use gson-utils:
Reader in = null;
Iterator<Book> it = GsonUtils.readListLazy(in, Book.class);
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I've got java.lang.ArrayIndexOutOfBoundsException: null exception in my application two times, both of them happened at list.get() method, the JDK source like this
`private E get(Object[] paramArrayOfObject, int paramInt)
{
return paramArrayOfObject[paramInt];
}
`
What's the problem?
please give some simple example which throws java.lang.ArrayIndexOutOfBoundsException null.
Thanks!
Please check array length before calling paramArrayOfObject[paramInt];
if(paramArrayOfObject.length > paramInt)
return paramArrayOfObject[paramInt];
else
return null;
This question already has answers here:
How to avoid "ConcurrentModificationException" while removing elements from `ArrayList` while iterating it? [duplicate]
(10 answers)
Closed 3 years ago.
I can be the implementation of a chat app to show users chat only the can get user_id to be sent or receive a message to store in user-list and select the user to be added in the list some error like a User list if added user 1 and add user 2 when and user 1 again the exception in ArrayList
for (String id : usersList)
if (equal(user.getId(), id)) {
if (mUseres.size() != 0) {
for (User user1 : mUseres) {
if (!equal(user.getId(), user1.getId())) {
mUseres.add(user);
}
}
} else {
mUseres.add(user);
}
The issue is here:
for (User user1 : mUseres) {
if (!equal(user.getId(), user1.getId())) {
mUseres.add(user);
}
You are iterating mUseres (for....) and at same time you are adding new items (mUseres.add)
You cant modify the List while iterating through it.
From the docs
Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I have to get the first element of an array., but it is possible that the element is empty; If the element is empty I put an empty field (I am trying to generate a pdf)
Here is my code now:
public void makePdf(Long id) throws IOException {
Candidacy ca = candidacyRepository.findOne(id);
cos.beginText();
cos.showText(
ca.getInterviews().stream().map(Interview::getAgency).map(Agency::getAgencyName).collect( Collectors.toList()).get(0)!=null?ca.getInterviews().stream().map(Interview::getAgency).map(Agency::getAgencyName).collect( Collectors.toList()).get(0):""));
cos.endText();
}
So I will wish not to prevent the generation of the pdf.
Thank you very much for your support!
UPDATE
Sorry for the lack of precision:
I also sort on the date.
public void makePdf(Long id) throws IOException {
Candidacy ca = candidacyRepository.findOne(id);
cos.beginText();
cos.showText(
ca.getInterviews().stream().sorted((a,b)-> a.getInterviewDate().compareTo(b.getInterviewDate())).sorted((a,f)->f.getInterviewDate().compareTo(a.getInterviewDate())).sorted((b,f)->b.getInterviewDate().compareTo(f.getInterviewDate())).map(Interview::getAgency).map(Agency::getAgencyName).collect( Collectors.toList()).get(0)!=null?ca.getInterviews().stream().sorted((a,b)-> a.getInterviewDate().compareTo(b.getInterviewDate())).sorted((a,f)->f.getInterviewDate().compareTo(a.getInterviewDate())).sorted((b,f)->b.getInterviewDate().compareTo(f.getInterviewDate())).map(Interview::getAgency).map(Agency::getAgencyName).collect( Collectors.toList()).get(0):""));
cos.endText();
}
I get a NullPointerException:/
Thank you for you help
This code doesn't make sense. You are executing the same Stream pipeline twice, and each time you generate an entire List when you only need the first element of that List.
You can use findFirst to get the first element of the Stream.
EDIT :
After testing my original answer, it turned out it doesn't work. findFirst() throws a NullPointerException if the first element is null.
You can avoid that by setting the default value before calling findFirst() :
ca.getInterviews().stream()
.map(Interview::getAgency)
.map(Agency::getAgencyName)
.map(s->s!=null?s:"") // this will replace null with ""
.firstFirst()
.get();
This question already has answers here:
Why doesn't this code throw a ConcurrentModificationException?
(3 answers)
Closed 7 years ago.
I have following code -
import java.util.ArrayList;
import java.util.List;
public class ArrayListTest {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1a");
list.add("2b");
for (String s : list) {
list.remove(s);
}
System.out.println("Removed");
System.out.println(list);
}
}
If I run this program, I expect it should throw exception but the output is "2b". If it is running fine then why It is not removing the last object.
Again If I add more element in the list It is thorwing exception java.util.ConcurrentModificationException Which is expected.
My question is -
Why it is not removing all the elements from list if we have 2 elements in the list ??
Why the java.util.ConcurrentModificationException exception occur only when we have more elements ?? I tried a lot of time with two elements.
I am using Java 8.
Thanks in advance.
Actually, when you are removing 1a from the list, the size is getting reduced, and you now have only 1 element in the list, thus the loop does not execute the second time, there by resulting in the keeping the second element.