• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to fix ArrayIndexOutOfBoundsException?  RSS feed

 
Mark Captain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am currently working on a program and receiving an ArrayIndexOutOfBoundsException error. I have never seen this before and do not know what it means or how to fix it.

This part of the code has each line from a notepad file taking up its own element of an array with a maximum of 30 elements

batters[0] = name singles doubles triples homeRuns atBats
batters[1] = name singles doubles triples homeRuns atBats
ect...



I am recieving this error on line 7

 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should consider what you want to do if

returns an array with fewer than 6 elements.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
println is your best friend. stick them in all over the place, with the expectation you'll take them out later.

I would say you need to put on in after your line 3 and print stats.length. It's (clearly) shorter than you think. maybe even stick in a for loop:


*above code is untested/validated, but is for illustrative purposes only.
 
Mark Captain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You should consider what you want to do if

returns an array with fewer than 6 elements.


What do you mean by this exactly? I was thinking that the array was already exactly 6 elements.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Captain wrote:What do you mean by this exactly? I was thinking that the array was already exactly 6 elements.


The split() method returns an array object. And there is nothing in the JavaDoc that says that the returned array must have at least 6 elements.

Regardless, why don't you just add an "if" check, to confirm that it is at least six elements first?

Henry
 
Mark Captain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Mark Captain wrote:What do you mean by this exactly? I was thinking that the array was already exactly 6 elements.


The split() method returns an array object. And there is nothing in the JavaDoc that says that the returned array must have at least 6 elements.

Henry


I have tried to add code creating the array having 6 elements and still am receiving the same error



 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Captain wrote:I have tried to add code creating the array having 6 elements and still am receiving the same error





The first line creates a 6 element array object and assigns it to the stats variable. The second line does a split, which returns an array, which is assigned to the stats reference variable. The array that was create at line one is no longer reachable, and hence, garbage collected.

In other words, the code that you added effectively does nothing.

Henry
 
Mark Captain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Mark Captain wrote:I have tried to add code creating the array having 6 elements and still am receiving the same error





The first line creates a 6 element array object and assigns it to the stats variable. The second line does a split, which returns an array, which is assigned to the stats reference variable. The array that was create at line one is no longer reachable, and hence, garbage collected.

In other words, the code that you added effectively does nothing.

Henry


Thank you for your help Henry, but i believe I am quite lost and am out of ideas on where to go with this.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should know what to do by now. As everybody has been telling you, you are assuming that the split() method is going to return an array with 6 elements. But obviously it isn't, since you get those exceptions. So your job is to find out why it doesn't. At the moment you appear to be in denial, or maybe you're at the bargaining stage now. But you have to get over that. It's returning an array with only two or three entries, which contradicts your expectations. To get some idea of why that might be happening, I suggest finding out what they are. Printing them out, as has already been suggested, would be a good start.

This sort of thing happens constantly when you're a programmer. You have some code which you think does X, but when you run it you find it doesn't do X. Sometimes that's because the code is wrong, and other times it's because you gave it some input which was wrong. And then it's your job to find out which.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!