• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayIndexOutOfBoundsException

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
Here's the code where I'm having the problem:
StringTokenizer st = new StringTokenizer (inputString, ";");
String wordArray [] = new String [numberOfTokens];
for (int count = 0; count < numberOfTokens; count++)
{
wordArray [count] = st.nextToken();
}
This generates an ArrayIndexOutOfBoundsException. Does anyone have any idea why? The SDK documentation says that it is normally caused by an array index being negative or too high for the array.
But this shouldn't be the case here, should it? I mean, wordArray should never be higher than the number of tokens in st, should it?
Any ideas or comments? Thanks!!
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mick, it's not apparent how your numberOfTokens property is being calculated. Is your example missing a line of code such as
int numberOfTokens = st.countTokens();
?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the value of numberOfTokens? Is it a local variable? Is it an instance variable?
Assuming that numberOfTokens is properly initialized, the only exception I see you getting with the code is NoSuchElementException thrown by nextToken(). You can guard against this exception by adding a check for st.hasMoreTokens() in the for-loop condition.
Junilu
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And besides this... don't use a for loop if you don't have to. Investiage the API for StringTokenizer, and look for " hasMoreTokens() " and think 'while' loop.
 
Mick Flynn
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scott, Junilu,
Sorry about that, I forgot to include the line:
int numberOfTokens = st.countTokens ();
(which you guessed correctly by the way).
And it throws this beauty:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at CardSet.<init>(FlashCardViewer.java:277)
at FlashCardViewer.importFile(FlashCardViewer.java:194)
at FlashCardViewer.<init>(FlashCardViewer.java:127)
at FlashCardViewer.main(FlashCardViewer.java:133)
Any ideas?
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested the code in the original post and it worked fine. My guess is that the conditional statement in your for loop is "count <= numberOfTokens" instead of "count < numberOfTokens".
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic