We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

String problem

incah
incah asked
on
Medium Priority
203 Views
Last Modified: 2012-05-04
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 ""?
Comment
Watch Question

Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
<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?

Commented:
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
cph

Commented:
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.

Author

Commented:
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.

Author

Commented:
Please forget the question. The exception was happening when StringBuffer.setCharAt() was called, and not on String.atChar(). My mistake. Sorry.
cph

Commented:
Well at least you found it :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.