Solved

String problem

Posted on 1998-08-03
7
189 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 ""?
0
Comment
Question by:incah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 1

Accepted Solution

by:
cph earned 50 total points
ID: 1230593
How did you define in? Is it like: String in;
or did you define an array of String?
In the first case if you init you string in to "abc", then in.chatAt(0); is perfectly valid.
However in the second case, you have to specify which index you want, eg in[0].charAt(0);

Also you should know that if you define a length to a string via some of the constructors then the max index if the length-1.

CpH
0
 

Author Comment

by:incah
ID: 1230594
<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?
0
 
LVL 1

Expert Comment

by:Sieger
ID: 1230595
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:cph
ID: 1230596
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.
0
 

Author Comment

by:incah
ID: 1230597
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.
0
 

Author Comment

by:incah
ID: 1230598
Please forget the question. The exception was happening when StringBuffer.setCharAt() was called, and not on String.atChar(). My mistake. Sorry.
0
 
LVL 1

Expert Comment

by:cph
ID: 1230599
Well at least you found it :)
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question