• Post Reply Bookmark Topic Watch Topic
  • New Topic

array out of bounds exception problem  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my code:


This is my error:
--------------------Configuration: <Default>--------------------
[Poster][ Ball ][Puzzle][ ][Poster]
[ ][Poster][ Game ][ ][ ]
[ ][ Game ][ ][ ][ ]
[ Ball ][ Ball ][Puzzle][ ][ Doll ]
[ ][ Doll ][ Doll ][ Game ][Puzzle]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at CoinPitch.determineWinners(CoinPitch.java:94)
at CoinPitch.<init>(CoinPitch.java:35)
at CoinPitchGame.main(CoinPitchGame.java:18)



I know where the error is located, but I am totally stumped on how to fix it.
Any suggestions?
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi erin topper,

Welcome to CodeRanch!

Its good to see that you have used code tags in your first post

Now, coming to your issue - ArrayIndexOutOfBoundsException - I assume that you know what this exception means and in which scenario it occurs.

I've not debugged your code, but looking at exception stack trace and other things, I could figure out below things:
1) Dimensions of gameBoard are 5x5
2) In method determineWinners, contents of gameBoard is accessed as gameBoard[coinTosses[r][0]][coinTosses[r][1]]
3) Values of coinTosses[r][0] and coinTosses[r][1] depend on length of coinTosses, which is 10
4) Thus, there is possibility of value of coinTosses[r][0] and/or coinTosses[r][1] being greater than or equal to NUMBERPRIZES.
5) And hence, ArrayIndexOutOfBoundsException.

As I've already said, I didn't go through code line by line and I don't have any idea about which game is this and what are the rules. But what I can suggest is : try to have uniform values for lengths of array. Also, I'm not sure why you are accessing locations of gameBoard depending on values of coinTosses.

I hope this helps.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's kind of hard to debug your code, since this is not the code that is running. The error tells you there was a problem on line 94 of your code...but the code you provide has a comment on line 94.

And since you have to have more code somewhere (you don't have a main() method), nobody here can even try running it to see what actually happens.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!