Strange message When a entity is killed in game

So there’s a really weird message that i’m going to post in here along with the entire code…sometimes when you kill an entity it doesn’t appear…but other times this message appears when you kill an entity…so I’m guessing somewhere I have a leak in the logic…Help please?

Code

fml Error Log

Sometimes the event seems to be thrown without any entity in the getEntities() collection, so calling event.getEntities().iterator().next() throws an Exception. I think it should not happen but to fix the issue you should iterate over all entities instead of only the first one. Use a loop : for (Entity entity : event.getEntities()) instead of Entity entity = event.getEntities().iterator().next();

As @Yeregorix says, you want to account for the possibility of null entities, or even no entities at all.

That code is untested but it should work + much more concise.

I believe that the problem is that you’re advancing the iterator twice. Once to get String a, and once to get the Entity. You should advance it once to get an entity object, and get the String from that object.

Calling getEntities().iterator() create a new Iterator copy each time, so the problem is not here.