• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help in tracking down coding error  RSS feed

 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have the following piece of code...........



Somewhere within this snippet of code I get the following error:-
java.lang.IndexOutOfBoundsException: Index: 2 Size: 2

Can anybody suggest what code I need to add so that I can come to grips with what is producing the error ?

j runs from 0 thru 6

Thank you
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somewhere within this snippet of code I get the following error:-
java.lang.IndexOutOfBoundsException: Index: 2 Size: 2

The code tried to use an index of 2 with an array (or String) that only had 2 elements. An index of 2 requires at least 3 elements.

Can you post the complete text of the error message. The stack trace that comes with it should show where the exception happened.
Make sure the catch block calls the printStackTrace method to show the stack trace.

Note: I don't see anywhere in the posted code where any index is used.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will get another error when you get to index 6; that entails a 7‑element array.
The == is being applied to primitives, not Strings.
There is something wrong with using arrays and non‑array fields.
 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you................

The only array I use in my large java program which has 2 elements is



and I have chosen NOT to use this array in the problem area, rather I use strings "Up" & "Down" instead


Maybe that is where I am going wrong?

To put the above code in perspective, it is a small part of a large subroutine which always goes smoothly when I first run the program
During the initialization process I evaluate seven data-mining models and write all seven to disc with NO errors

During the next run, the program chooses whether to write the new model TO disc or read the previously saved model FROM disc
and it is during this run that the error pops up - usually on the THIRD of seven models (counter j= 0 to 6)
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder how you get the IndexOutOfBoundsException if you are not using an array.
The stack trace should show the statement where the exception happened.
 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excuse my total ignorance BUT am unclear where to find the Stack Trace

Please understand that I am running my java program from within Dukascopy's JForex Platform

As soon as this error pops up - my main j=0 thru 6 loop collapses and exits
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where to find the Stack Trace

If the statement that throws the exception is inside of a try{}catch block, the catch block should have a call to the printStackTrace method:
The stack trace:
shows the exception happened at line 569 in TestCode22 where it called the method: charAt
 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes - I understand what you are saying BUT the only printout message I get is the one-liner as I quoted above

I do have code as you show to print stacktrace but am unclear where the result is to be found ?

e.g. I have the following code further up



OK - so I am NOT actually printing stacktrace - is that MY problem ?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running my java program from within Dukascopy's JForex Platform

You will have to ask your questions on a forum for the platform your code is on. I expect it is not pure java and that there are other components to that platform that effect what happens to code that is run there,

If you are working with a complete java program with classes and methods, copy it and paste it here.

Note: There should be a call to the printStackTrace method at line 10. The println statement does not show the stack trace.
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Matthews wrote:OK - so I am NOT actually printing stacktrace - is that MY problem ?

Yes - that will cause only the exception message to be printed, no the stack trace.
 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Norm

Many thanks for persisting
I now see how to code to get a stacktrace - that should assist me immensely

Many thanks for your time
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!