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

String problem

Posted on 1998-08-03
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 ""?
Question by:incah
  • 3
  • 3

Accepted Solution

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.


Author Comment

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?

Expert Comment

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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


Expert Comment

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.


Author Comment

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.

Author Comment

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

Expert Comment

ID: 1230599
Well at least you found it :)

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 10 IE Certificate Issue 10 51
expectj telnet failing 5 45
hibernate jars 4 45
Android development question 2 54
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

839 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