I’m making a game but it sometimes crashes when i press play.
Here’s the code. Hopefully I can be helped
My error is
Exception in thread “Thread-2” java.lang.IndexOutOfBoundsException: Index: 219, Size: 2
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.GamerC4.main.Handler.render(Handler.java:25)
at com.GamerC4.main.Game.render(Game.java:200)
at com.GamerC4.main.Game.run(Game.java:104)
at java.lang.Thread.run(Unknown Source)
~1,400 lines is wayyyyyyyy too much code.
If you looked at the stack trace, you could see that the exception comes from [icode]at com.GamerC4.main.Handler.render(Handler.java:25) at com.GamerC4.main.Game.render(Game.java:200) at com.GamerC4.main.Game.run(Game.java:104)[/icode]so you’d probably be better off posting those classes instead of the whole thing.
As DarkCart said, 1400+ lines of code is excessive. There’s even an message that says
at the bottom of the text box.
I would shove it all into a pastebin now. It’s fairly repulsive to just see a wall of text at the start of the message, and it might turn some people away if they were going to help you.
The stack trace shows at line 25 in your Handler class, you’re attempting to get an index that doesn’t exist/is too large. Is it possible something may be changing the list concurrently?
I already went over what you were doing wrong in this thread: http://www.java-gaming.org/topics/i-get-a-java-lang-nullpointerexception-i-cant-fix/36564/view.html
chrislo27 is correct, you’re modifying the ArrayList concurrently.
You can’t be modifying game state like that in your event handlers, because those get run on the event dispatch thread, whilst you’re reading the list in the game loop thread. Game over.
Sorry but what do I do exactly
How do i use the pastebin
I just put my code in it and posted
Sorry but can you see my code
Also what is the solution to my issue
I know you guys are giving me great answers but could i have a step by step explanation telling me what to do
First: we can’t see your code. All I can see is a one-line empty code box.
Second: giving you a step-by-step explanation isn’t our duty. We’re here to help you solve issues and to help you learn. Holding your hand all the way through doesn’t make you a better programmer, learning the why of what something does this is what does.
As BurntPizza and I stated, you’re modifying the array concurrently. You need to find where you’re doing that in the event handlers (they’re on a separate thread) and move it to your main/work thread.
I don’t get it. I really do apologize for wasting your time and I know you must be wondering how I function as a human being if I don’t understand your explanation , but I really don’t understand
From what I understand I should the thing that changes my Game state to my main thread
We get that, but if we just tell you the answer, you’ll never learn anything.
Can I just tell you an idea to a solution and you will tell me if it’s right or not
Do I move the things that change the game state to my tick method in main?
When an event is triggered (like in your key input methods, for example) it’s dispatched on a different thread than the one with your handler class. That means if you’re adding/removing objects in the event methods on the event dispatch thread, the contents may differ from the one on the logic thread, for example. You need to stick to one thread, preferably the logic thread.
As for your pastebin issue, just put it on the pastebin and put the pastebin link here so we can view it.
I sort of get it now
I’m moving my menu event handlers to my handler class
would that work
Try it, and see if it does
Oops now im just creating new errors