Please, I am going insane: First element access causing ArrayIndexOutOfBoundsException:0

H

homerjk

Hello there,

Please God can someone help me before I break my PC.

I have a class that runs a query against an Oracle 10g database. The
query is returning the results I want.
I get two users which is what I want. I have an array created that has
a length of two.
I feed the results of the query into the array...
<code>
int[] engineers = new int[2];
String sql = "select USERID from g_user where role='Engineer'";
PreparedStatement pstmt = tempConn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
engineers = rs.getInt("USERID"); // find result of count
i++;
}
</code>

The very next thing if i try
<code>
System.out.println("engineer array length is " + engineers.length);
System.out.println("engineer array 0 is " + engineers[0]);
System.out.println("engineer array 1 is " + engineers[1]);
</code>

I get....
<code>
engineer array length is 2
java.lang.ArrayIndexOutOfBoundsException: 0
engineer array 0 length is 85
engineer array 1 length is 123
</code>

If i take out the first print message to find the length i get the
same error when i try to access the first element. If i take out the
message to access the length and the first element i get no error
whatsoever! but obviously i cannot do anything useful with my program
because i cannot access the first element.

I thought i was losing my mind, so i created a tiny program with just
this code on its own and it runs perfectly which is making me even
crazier!
What in the name of all that is good could be causing this?! I mean it
is printing out the correct contents and length of the array even
after it shows an error to say outofbounds!

Any help at all is very much appreciated...

Thank you,
J.
 
D

david.karr

Hello there,

Please God can someone help me before I break my PC.

I have a class that runs a query against an Oracle 10g database. The
query is returning the results I want.
I get two users which is what I want. I have an array created that has
a length of two.
I feed the results of the query into the array...
<code>
int[] engineers = new int[2];
String sql = "select USERID from g_user where role='Engineer'";
PreparedStatement pstmt = tempConn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
engineers = rs.getInt("USERID"); // find result of count
i++;}

</code>

The very next thing if i try
<code>
System.out.println("engineer array length is " + engineers.length);
System.out.println("engineer array 0 is " + engineers[0]);
System.out.println("engineer array 1 is " + engineers[1]);
</code>

I get....
<code>
engineer array length is 2
java.lang.ArrayIndexOutOfBoundsException: 0
engineer array 0 length is 85
engineer array 1 length is 123
</code>

If i take out the first print message to find the length i get the
same error when i try to access the first element. If i take out the
message to access the length and the first element i get no error
whatsoever! but obviously i cannot do anything useful with my program
because i cannot access the first element.

I thought i was losing my mind, so i created a tiny program with just
this code on its own and it runs perfectly which is making me even
crazier!
What in the name of all that is good could be causing this?! I mean it
is printing out the correct contents and length of the array even
after it shows an error to say outofbounds!


A few things:

There's a slight disconnect between the code you say you're executing
and the output you're getting, so either you just typed it into this
message wrong, or you're not executing the code you think you are.
For instance, you reference a code line of this:

System.out.println("engineer array 0 is " + engineers[0]);

and an output line of this:

engineer array 0 length is 85

In the code, you don't say "length", but it's in the output. Again,
this could be a trivial error in the input of this message, or it
could be telling. Figure that out.

Second, you don't see the stack trace from that exception, so it's
hard to tell for certain where it's coming from. You could wrap all
of your code in a specific try/catch for that exception and print out
the stack trace. That might give you more information. If you add
that code, and the output doesn't change at all, then somehow that
exception is happening somewhere else entirely.

Third, if you say you've extracted this code into an isolated test
case, and it doesn't demonstrate the problem, then what are you doing
differently? Is the code that creates and populates the "engineers"
list really right before the code that prints out the values, or are
they perhaps in two different scopes (or threads?) where the
"engineers" list isn't populated yet?
 
E

Eric Sosman

homerjk wrote On 03/08/07 14:53,:
Hello there,

Please God can someone help me before I break my PC.

I have a class that runs a query against an Oracle 10g database. The
query is returning the results I want.
I get two users which is what I want. I have an array created that has
a length of two.
I feed the results of the query into the array...
<code>
int[] engineers = new int[2];
String sql = "select USERID from g_user where role='Engineer'";
PreparedStatement pstmt = tempConn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
engineers = rs.getInt("USERID"); // find result of count
i++;
}
</code>

The very next thing if i try
<code>
System.out.println("engineer array length is " + engineers.length);
System.out.println("engineer array 0 is " + engineers[0]);
System.out.println("engineer array 1 is " + engineers[1]);
</code>

I get....
<code>
engineer array length is 2
java.lang.ArrayIndexOutOfBoundsException: 0
engineer array 0 length is 85
engineer array 1 length is 123
</code>

If i take out the first print message to find the length i get the
same error when i try to access the first element. If i take out the
message to access the length and the first element i get no error
whatsoever! but obviously i cannot do anything useful with my program
because i cannot access the first element.

I thought i was losing my mind, so i created a tiny program with just
this code on its own and it runs perfectly which is making me even
crazier!
What in the name of all that is good could be causing this?! I mean it
is printing out the correct contents and length of the array even
after it shows an error to say outofbounds!


No, it's not. The final two lines of output must
be coming from somewhere else altogether, for two reasons:

First, when the exception is thrown (from whereever it
is thrown) the normal flow of control in your code is broken
off and execution resumes in a catch clause somewhere further
up the stack. This means your second and third println()
calls never execute at all (or if the exception is thrown
while constructing the String to be printed by the first,
that call is aborted and produces no output). Either way,
none of the output comes from the second two println()s.

Second, another and perhaps easier way to see that the
second two println()s don't produce the output is to note
that the output contains the substring "length " which the
println() calls do not generate. The output comes from
some entirely different piece of code.

Since the mysterious output comes from something you
haven't revealed, I'm at a loss to explain it.
 
H

homerjk

No, it's not. The final two lines of output must
be coming from somewhere else altogether, for two reasons:

First, when the exception is thrown (from whereever it
is thrown) the normal flow of control in your code is broken
off and execution resumes in a catch clause somewhere further
up the stack. This means your second and third println()
calls never execute at all (or if the exception is thrown
while constructing the String to be printed by the first,
that call is aborted and produces no output). Either way,
none of the output comes from the second two println()s.

Second, another and perhaps easier way to see that the
second two println()s don't produce the output is to note
that the output contains the substring "length " which the
println() calls do not generate. The output comes from
some entirely different piece of code.

Since the mysterious output comes from something you
haven't revealed, I'm at a loss to explain it.

Hi Guys,

Thanks for the replies.

Apologies for the mix up over what was being printed out, I typed that
out myself and just copied the first statement and pasted it out the
other two times and changed it to have the right output.

Its sorted now anyway. I re-wrote the whole thing from scratch and I
think the problem may have been coming from the use/re-use of the
prepared statements and resultsets. I gave each section its own
prepared statement and resultset and they problem now does not occur
at all. Still not too sure why though as before hand I was clearing
out the prepared statements each time...

Regardless, it is working now and I will be able to sleep tonight!
Thank you for you suggestions,
J.
 
H

homerjk

homerjk wrote On 03/08/07 14:53,:


Hello there,
Please God can someone help me before I break my PC.
I have a class that runs a query against an Oracle 10g database. The
query is returning the results I want.
I get two users which is what I want. I have an array created that has
a length of two.
I feed the results of the query into the array...
<code>
int[] engineers = new int[2];
String sql = "select USERID from g_user where role='Engineer'";
PreparedStatement pstmt = tempConn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
engineers = rs.getInt("USERID"); // find result of count
i++;
}
</code>

The very next thing if i try
<code>
System.out.println("engineer array length is " + engineers.length);
System.out.println("engineer array 0 is " + engineers[0]);
System.out.println("engineer array 1 is " + engineers[1]);
</code>
I get....
<code>
engineer array length is 2
java.lang.ArrayIndexOutOfBoundsException: 0
engineer array 0 length is 85
engineer array 1 length is 123
</code>
If i take out the first print message to find the length i get the
same error when i try to access the first element. If i take out the
message to access the length and the first element i get no error
whatsoever! but obviously i cannot do anything useful with my program
because i cannot access the first element.
I thought i was losing my mind, so i created a tiny program with just
this code on its own and it runs perfectly which is making me even
crazier!
What in the name of all that is good could be causing this?! I mean it
is printing out the correct contents and length of the array even
after it shows an error to say outofbounds!

No, it's not. The final two lines of output must
be coming from somewhere else altogether, for two reasons:

First, when the exception is thrown (from whereever it
is thrown) the normal flow of control in your code is broken
off and execution resumes in a catch clause somewhere further
up the stack. This means your second and third println()
calls never execute at all (or if the exception is thrown
while constructing the String to be printed by the first,
that call is aborted and produces no output). Either way,
none of the output comes from the second two println()s.

Second, another and perhaps easier way to see that the
second two println()s don't produce the output is to note
that the output contains the substring "length " which the
println() calls do not generate. The output comes from
some entirely different piece of code.

Since the mysterious output comes from something you
haven't revealed, I'm at a loss to explain it.


Guys, thanks for your replies, I wrote a long winded reply but it got
lost somewhere along the way.

Apologies for the confusion over the println's, this was a mistake, I
typed these out myself and this was not the actual program output.

The problem is now fixed, I re-wrote the program from scratch and I
suspect the problem was coming from the use/re-use of the prepared
statements and resultsets. On re-writing the code I gave each section
its own prepared statement and resultset and it fixed the problem. I'm
still not 100% sure as to why this was causing the problem as I was
clearing out the prepared statements before re-using them.

Regardless, the program is working now and I will be able to sleep
tonight.

Thank you,
J.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top