• Post Reply Bookmark Topic Watch Topic
  • New Topic

Two list class variables used at same time causing array out of bounds exception  RSS feed

 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two java.awt.list variables called list1 and list2. I have two arrays of type String which are equal length and are never null (contain static data every time the program is run). I have a loop running from 0 to the end of ethe arrays (both of size 984).

I am getting java.lang.ArrayIndexOutOfBoundsException numerous times as this loop runs.. at different element numbers each time it is run.

However, if I set either list#.add to put in the array[0] it runs through flawlessly... this proves both arrays are holding the correct data. I only get the error when both are going through each array.

Even moved the two list#.add lines into individual loops but the same errors continued.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried printing the length of the two arrays before looping?Once you get an exception, does each loop iteration throw the same exception, or is the exception thrown only for some elements within the loop (which should be impossible unless you're changing the array references)?
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If its not too important that the two lists should be populated in the same loop you could try the below code



ram.
 
Marilyn de Queiroz
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is usually better to do a for loop by using the array.length rather than a hard coded number. I suggest you use
for ( int loop1 = 0; loop1 < display1.length ; loop1++ )
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me say thank you first for all your replies.
Originally posted by David Harkness:
Have you tried printing the length of the two arrays before looping?Once you get an exception, does each loop iteration throw the same exception, or is the exception thrown only for some elements within the loop (which should be impossible unless you're changing the array references)?

The array lengths are static as I initialize both arrays to size 1000 as well as place a "" in every field before it ever gets to this portion of my code. The loop length on the other hand is not.. I have a count variable which is where the loop actually goes to, this was just an example so I figured I would hard code it as to avoid unnecessary code.
Originally posted by ramprasad madathil:
If its not too important that the two lists should be populated in the same loop you could try the below code
ram.

It is important.. but regardless, I did try what you said yesterday and each loop still randomly errors with the same exception. When I say "random" I mean it's in a different place every time I run the code using the same data file.

Please keep the suggestions coming.. I checked my memory available via the Task Manager on Windows XP Professional and I seem to have plenty free so that is not the issue.

Thank you.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Humor us: add these lines right before this loop:

System.out.println("Display1: " + display1.length);
System.out.println("DisplayConvertedFull: " + displayConvertedFull.length);

Run the program, tell us what it prints.
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Humor us: add these lines right before this loop:

System.out.println("Display1: " + display1.length);
System.out.println("DisplayConvertedFull: " + displayConvertedFull.length);

Run the program, tell us what it prints.

Sure thing.. both came back with 10000.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you hard-coding your 984 value? Or is this the expected value of a variable?
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This seems to be a decent work around.

Assumed that if the code gets to the loop1++ then it has successfully added to both lists. However, my only concern is that if the list1 add is successful but list2 add is not, then list1 will have an extra row potentially every time the list2 fails... but so far that scenario has not occurred.

FWIW, list1 is holding ASCII values which are not standard characters. I do not feel this is a problem as the characters are the same every time the file is read, but the exception occurs at a different line every time (or not at all in some cases).
[ March 01, 2005: Message edited by: Jeff Grant ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

984 is the total number of lines read in from a file. I just simplified my example by hard coding it for you.

Please don't simplify it for our case. Can you show us how you assign this value? Could you show the (however many) lines before you get to the loop which assigns values to your arrays and sets this variable which you believe to be 984?


FWIW, list1 is holding ASCII values which are not standard characters. I do not feel this is a problem as the characters are the same every time the file is read, but the exception occurs at a different line every time (or not at all in some cases).

What you are storing in these lists is immaterial. No method in java.awt.List throws an ArrayIndexOutOfBounds exception so it is not adding things to these Lists which causes the exception.
[ March 01, 2005: Message edited by: Paul Sturrock ]
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is literally as simple as that.. totalLines = 984 is output to the prompt so I know that is correct.

I display this java.awt.list on the screen and scroll down.. I have a listener which tells me what line I click when I click it in either list (list1 - unconverted, list2 - converted). Both have the correct number of lines.

I can also confirm the number of lines by opening the unconverted file in a text editor.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How big is your class? Is it practical to post the whole thing here?
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
How big is your class? Is it practical to post the whole thing here?

It's only about 1300 lines.. not the biggest, but I don't think we'd want it all here.

I guess that since it doesn't seem like an obvious problem to anyone, I'll keep with my "while" work around as it doesn't seem to be causing any inconsistancies.. thanks for trying to help.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm totally perplexed how it fails since the if and while loops you posted are equivalent given your revised for loop. Very strange.

Do you always get the exception accessing the same array, and do you get exceptions for every element after the failed one in the same array? In other words, if element 10 is out of bounds, 11, 12, ... must also be out of bounds and throw exceptions.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jeff Grant

The array lengths are static as I initialize both arrays to size 1000...

and later......

The loop length on the other hand is not.. I have a count variable which is where the loop actually goes to........



The only thing that I can see that may have cause this error is that the loop length exceeds the array length.

Are you sure loop length returns 984 always?

If


had been


,it would never ever throw an ArrayIndexOutOfBoundsException provided there are no loop increment expressions inside your loop.

ram.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!