Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range:

Why do I receive a out of bounds error when using substring?

String timestamp= line.substring(1, 10);

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range:

How can I avoid this, I need to somehow check the position for null with out the thing blowing up.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

How can I check for null in a file while using substring?
It's not possible to get null from a file.

But in any case, the exception you cite is something quite different - an indexing problem. You're making assumptions about the value of 'line' that are incorrect. You might as well check if 'line' is null though i don't see how it could be, but more importantly, you need to check your assumption about its content, probably by at least checking its length

if (line != null && line.length() >= 11) {
  // Do it, but don't use the magic number like i just used

Open in new window

softwarelovasAuthor Commented:
Hmm.. I believe what I need to do is determine why I am receiving a out of bounds error.
Post the FULL stack trace error here. If you don’t fancy that, then look for the bounds error int in the error trace, and compare it to the length of your substring term.
awking00Information Technology SpecialistCommented:
What is the value of line and how is that determined? What are you trying to do with the substring? Remember that indexes of characters in a string are zero-based (i.e. the first character has an index of 0) so, if line has fewer than 11 characters (e.g. a length of 10), you will get the index out of bounds error if your substring ending index is >= 10 since the last character in a 10-character string would have an index of 9.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I won't put words into anyone's mouth, but I think that the way you expressed your search for a 'null' element might have led CEHJ to think that that 'null' was from a File, and if so, I can see why that happened. But he DID say it was an indexing error  - which it is - and in that sense I'd be grateful if you would award CEHJ the points rather than me this time. Up to you of course, but this is my comment. ; )
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.