Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Index Out of Bounds Exception

 
sravanthi yendluri
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am running a servlet on Tomcat in Windows XP.I am actually connecting Midlets with Oracle 9i using the servlets .

When I run the J2ME Wireless Toolkit, in the console I get an error saying,

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

Why am I getting this error?

Thanks,
Sravanthi.
 
Campbell Ritchie
Sheriff
Posts: 51413
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to go back to where it occurred and find out how you are putting the index in. Then find where you get the index from. A String index of -1 often means you have sought a char or a substring and not found it.
 
sravanthi yendluri
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using String userid=""
When I changed to static String userid1="", I am getting the same error.

What should I change the string to?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the line of code that actually throws the index out of bounds error?
 
sravanthi yendluri
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while((i=din.read())!=-1)
{
response=response+(char)i;
System.out.println("*************");
}
System.out.println("response is :: "+response);
int p=response.indexOf(':');
userid1 = response.substring(0,p);
System.out.println("userid is :: "+userid1);

I used userid1 as both global variable and even inside the class but it was giving me the same exception.
[ June 02, 2008: Message edited by: sravanthi yendluri ]
 
Campbell Ritchie
Sheriff
Posts: 51413
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plese use code tags (button below the message window) on quoted code; it makes it much easier to read.

If you don't have a ; inside your String then the indexOf(";") will come out as -1.

If you try to get a substring and pass -1 as a parameter it is outwith the bounds of the String . . . voilà an Exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic