Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why always this exception?

 
Annina WG
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meet with this exception today:
java.lang.StringIndexOutOfBoundsException: String index out of range: 8
at java.lang.String.substring(+29)
at chatmidlet.frmLogin.CommandPerformed(+166)
at chatmidlet.frmLogin$1.commandAction(+9)
at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+152)
at com.sun.kvem.midp.lcdui.EmulEventHandler$EventLoop.run(+427)
*************
my code respectively is:
public void CommandPerformed (Command c, Displayable d) {
if (c.getLabel() == "Login") {
try {
String Server = txtServer.getString() + ":" + txtPort.getString();
thisMIDlet.connect(Server);
thisMIDlet.send(new String ("login: " + txtNick.getString()));
String answer = new String(thisMIDlet.read());
if (answer.substring(0,8).compareTo("login ok") == 0) {
//get answer from Server,goto another Form
//Nickname and Server im MIDlet store
thisMIDlet.setServer(Server);
thisMIDlet.setNickname(txtNick.getString());
Display.getDisplay(thisMIDlet).setCurrent(thisMIDlet.getContent());
}
else {
//server sends error message back, 5 seconds
Alert alrtError = new Alert ("Error", answer.substring(0), null, AlertType.ERROR);
alrtError.setTimeout(5000);
Display.getDisplay(thisMIDlet).setCurrent(alrtError, this);
}//endif
thisMIDlet.disconnect();
}
catch (IOException e) {
// server not available
Alert alrtError = new Alert ("Error", "No Server available", null, AlertType.ERROR);
alrtError.setTimeout(5000);
Display.getDisplay(thisMIDlet).setCurrent(alrtError, this);
}
}
}
**********
the strange thing is this exception appears only recently after many times test,even when I shut down the server and expect to get the "No Server available" error message.
I use JBuilder 5 to compile and run this MIDLet.
Thanks very much for your adivce.
[ January 09, 2003: Message edited by: Annina WG ]
 
Annina WG
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it seems
Conn = (HttpConnection) Connector.open("Server Url");
called by
thisMIDlet.connect(Server);
doesn't raise any Exception.
How can I make it raise exception? Thanks!
 
Sumit Amar
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Annina, the exception is being caused by your substring(0,8) method, when it doesn't receive any input from the screen or apparantly when the string is small.
I hope that solves.
 
Annina WG
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know what this exception message mean. But I wonder how this exception can happen.
I catch "IOException when there is no server. If the exception is raised, as the logic of the program "substring(0,8)" cannot be examed.
Sumit,thank you anyway.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!