Link to home
Start Free TrialLog in
Avatar of incah
incah

asked on

String problem

I have a String named in. When I call in.charAt(0) I get StringIndexOutOfBoundsException. Why is this happening when the String is neither null nor ""?
ASKER CERTIFIED SOLUTION
Avatar of cph
cph

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of incah
incah

ASKER

<in> is a String (not array of Strings) parameter in a method, and when this method is called, <in> is coming from a database via ResultSet.getString() . Any other ideas?
Did you call the next method before assigning the String?
Let's say you have a variable called result as ResultSet,

you have to do result.next() first, then you can assign in to result.getString() if there's any row returned
So you have something like, in=ResultSet.getString();
ResultSet is not null, 'cos you'd get an Null pointer exception... (but it's still worth a check)
Have you try to get the length() of the string, ie in.length(); and see what's the value.

Chris.
Avatar of incah

ASKER

The string is not null and has a length because as my method is written it wouldn't reach to the point where in.charAt() is called. Anyway I printed the length to the standard output and it is 4.
Avatar of incah

ASKER

Please forget the question. The exception was happening when StringBuffer.setCharAt() was called, and not on String.atChar(). My mistake. Sorry.
Well at least you found it :)