Solved

String problem

Posted on 1998-08-03
7
184 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
  • 3
  • 3
7 Comments
 
LVL 1

Accepted Solution

by:
cph earned 50 total points
Comment Utility
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
Comment Utility
<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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:cph
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Well at least you found it :)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now