Re: Please, I am going insane: First element access causing ArrayIndexOutOfBoundsException:0
- From: "homerjk" <jonathan.keenan@xxxxxxxxx>
- Date: 8 Mar 2007 13:25:34 -0800
On Mar 8, 8:27 pm, Eric Sosman <Eric.Sos...@xxxxxxx> wrote:
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[i] = 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.
--
Eric.Sos...@xxxxxxx
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.
.
- References:
- Prev by Date: Re: Hey! i've done a Free Global Bad Words Dictionary,
- Next by Date: Re: String based hashCode
- Previous by thread: Re: Please, I am going insane: First element access causing ArrayIndexOutOfBoundsException:0
- Next by thread: Is Set faster than List
- Index(es):
Relevant Pages
|