• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why always this exception?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic