Pessoal, como primeiro programa em j2me eu to fazendo um gerador de serial pra aprender a manipular string e inteiro.
A ideia é gerar o serial a partir de um ID, entao o programa é simples… eu vou digitar esse ID no celular e ele vai me dar o serial.
Coloquei abaixo um método ficticio mas que apresenta o mesmo problema. Ele compila normalmente mas quando vou executar ele dá o erro em tempo de execução. To colocando abaixo o meu codigo e o erro:
Olhem o codigo:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.netbeans.microedition.lcdui.SplashScreen;
public class TesteMIDlet extends MIDlet implements CommandListener {
private boolean midletPaused = false;
private String senha = "";
private String serial = "";
private String digito = "";
private String nr1 = "";
private String nr2 = "";
private String nr3 = "";
private String nr4 = "";
private String nr5 = "";
private SplashScreen splashScreen;
private TextBox textBox;
private Command exitCommand;
private Command backCommand;
private Command screenCommand;
private Command backCommand1;
private Command Gerar;
private Command Limpar;
private Image image;
private Font font1;
public TesteMIDlet() {
}
public String geraSerial(String id){
serial = "";
digito = "";
nr1 = "0";
nr2 = "0";
nr3 = "0";
nr4 = "0";
//calcula o digito
digito = Integer.toString(Character.digit(id.charAt(0), 10)+Character.digit(id.charAt(1), 10)+Character.digit(id.charAt(2), 10)+
Character.digit(id.charAt(3), 10));
if (Integer.valueOf(digito).intValue() > 9) { digito = Integer.toString(Character.digit(digito.charAt(0), 10)+Character.digit(digito.charAt(1), 10));};
//multiplica o digito por todos os numeros
nr1 = Integer.toString(Integer.valueOf(digito).intValue()*(Character.digit(id.charAt(0), 10)));
if (Integer.valueOf(nr1).intValue() > 9) { nr1 = Integer.toString(Character.digit(nr1.charAt(0), 10)+Character.digit(nr1.charAt(1), 10)); };
nr2 = Integer.toString(Integer.valueOf(digito).intValue()*(Character.digit(id.charAt(1), 10)));
if (Integer.valueOf(nr2).intValue() > 9) { nr2 = Integer.toString(Character.digit(nr2.charAt(0), 10)+Character.digit(nr2.charAt(1), 10)); };
nr3 = Integer.toString(Integer.valueOf(digito).intValue()*(Character.digit(id.charAt(2), 10)));
if (Integer.valueOf(nr3).intValue() > 9) { nr3 = Integer.toString(Character.digit(nr3.charAt(0), 10)+Character.digit(nr3.charAt(1), 10)); };
nr4 = Integer.toString(Integer.valueOf(digito).intValue()*(Character.digit(id.charAt(3), 10)));
if (Integer.valueOf(nr4).intValue() > 9) { nr4 = Integer.toString(Character.digit(nr4.charAt(0), 10)+Character.digit(nr4.charAt(1), 10)); };
serial = nr1+nr2+nr1+nr4+nr3+nr2+nr1+nr2+nr3;
return serial;
}
private void initialize() {
}
public void startMIDlet() {
switchDisplayable(null, getSplashScreen());
}
public void resumeMIDlet() {
}
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
}
public void commandAction(Command command, Displayable displayable) {
if (displayable == splashScreen) {
if (command == SplashScreen.DISMISS_COMMAND) {
switchDisplayable(null, getTextBox());
}
} else if (displayable == textBox) {
if (command == Gerar) {
senha = textBox.getString();
textBox.setString("");
textBox.setString("Serial: "+geraSerial(senha));
} else if (command == Limpar) {
textBox.setString("");
} else if (command == backCommand1) {
exitMIDlet();
}
}
}
public SplashScreen getSplashScreen() {
if (splashScreen == null) {
splashScreen = new SplashScreen(getDisplay());
splashScreen.setTitle("splash");
splashScreen.setCommandListener(this);
splashScreen.setFullScreenMode(true);
splashScreen.setImage(getImage());
splashScreen.setText("Gerador de Serial");
splashScreen.setTextFont(getFont1());
splashScreen.setTimeout(2000);
}
return splashScreen;
}
public Image getImage() {
if (image == null) {
try {
image = Image.createImage("/imagem.png");
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
return image;
}
public Font getFont1() {
if (font1 == null) {
font1 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
}
return font1;
}
public Command getBackCommand() {
if (backCommand == null) {
backCommand = new Command("Voltar", Command.BACK, 0);
}
return backCommand;
}
public Command getExitCommand() {
if (exitCommand == null) {
exitCommand = new Command("Sair", Command.EXIT, 0);
}
return exitCommand;
}
public Command getScreenCommand() {
if (screenCommand == null) {
screenCommand = new Command("Telax", Command.SCREEN, 0);
}
return screenCommand;
}
public TextBox getTextBox() {
if (textBox == null) {
textBox = new TextBox("Gerador de Serial", "", 100, TextField.NUMERIC);
textBox.addCommand(getBackCommand1());
textBox.addCommand(getGerar());
textBox.addCommand(getLimpar());
textBox.setCommandListener(this);
}
return textBox;
}
public Command getBackCommand1() {
if (backCommand1 == null) {
backCommand1 = new Command("Voltar", Command.BACK, 0);
}
return backCommand1;
}
public Command getGerar() {
if (Gerar == null) {
Gerar = new Command("Gerar", Command.SCREEN, 0);
}
return Gerar;
}
public Command getLimpar() {
if (Limpar == null) {
Limpar = new Command("Limpar", Command.SCREEN, 0);
}
return Limpar;
}
public Display getDisplay () {
return Display.getDisplay(this);
}
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
public void startApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
public void pauseApp() {
midletPaused = true;
}
public void destroyApp(boolean unconditional) {
}
}
Olhem o erro:
Copying 1 file to C:\exemplos\Teste\dist\nbrun2184405215962360535
Copying 1 file to C:\exemplos\Teste\dist\nbrun2184405215962360535
Jad URL for OTA execution: http://localhost:8082/servlet/org.netbeans.modules.mobility.project.jam.JAMServlet/C%3A/exemplos/Teste/dist//Teste.jad
Starting emulator in execution mode
Installing suite from: http://127.0.0.1:49929/Teste.jad
TRACE: <at java.lang.IllegalArgumentException>, Exception caught in Display class
java.lang.IllegalArgumentException
at javax.microedition.lcdui.TextField.setCharsImpl(), bci=127
at javax.microedition.lcdui.TextField.setString(), bci=37
at javax.microedition.lcdui.TextBox.setString(), bci=5
at TesteMIDlet.commandAction(TesteMIDlet.java:153)
at javax.microedition.lcdui.Display$ChameleonTunnel.callScreenListener(), bci=46
at com.sun.midp.chameleon.layers.SoftButtonLayer.processCommand(), bci=74
at com.sun.midp.chameleon.layers.SoftButtonLayer.commandSelected(), bci=11
at com.sun.midp.chameleon.layers.MenuLayer.pointerInput(), bci=170
at com.sun.midp.chameleon.CWindow.pointerInput(), bci=76
at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handlePointerEvent(), bci=19
at com.sun.midp.lcdui.DisplayEventListener.process(), bci=296
at com.sun.midp.events.EventQueue.run(), bci=179
at java.lang.Thread.run(Thread.java:619)
javacall_lifecycle_state_changed() lifecycle: event is JAVACALL_LIFECYCLE_MIDLET_SHUTDOWNstatus is JAVACALL_OK