Forums Register Login

Why always this exception?

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
My pie came with a little toothpic holding up this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 913 times.
Similar Threads
MIDP Canvas problem!!!
How to handle SMS in J2ME
null pointer exception
easy fundamental oop problem
J2ME error when building application
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:51:45.