[J2ME] Erro ao converter String para double e mostrar valor no J2ME

Salve… eu procurei esse erro em város e lugares e como não consegui nada decidi postar aqui… o código que eu crei para o celular é esse:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;

public class principal extends MIDlet implements CommandListener {
    
    private boolean midletPaused = false;
    double n1, n2, n3;
    
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
    private Form formInserir;
    private TextField campo1;
    private TextField campo3;
    private TextField campo2;
    private Alert alert;
    private Command okCommand;
    private Command exitCommand;
    private Command okCommand1;
//</editor-fold>
    public principal() {
    }

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">
    /**
     * Initializes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {
        // write pre-initialize user code here
 
        // write post-initialize user code here
    }
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {
        // write pre-action user code here
        switchDisplayable(null, getFormInserir());
        // write post-action user code here
    }
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {
        // write pre-action user code here
 
        // write post-action user code here
    }
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
        // write pre-switch user code here
        Display display = getDisplay();
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }
        // write post-switch user code here
    }
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: formInserir ">
    /**
     * Returns an initiliazed instance of formInserir component.
     * @return the initialized component instance
     */
    public Form getFormInserir() {
        if (formInserir == null) {
 // write pre-init user code here
            formInserir = new Form("Inserir Contas", new Item[]{getCampo1(), getCampo2(), getCampo3()});
            formInserir.addCommand(getOkCommand());
            formInserir.addCommand(getExitCommand());
            formInserir.setCommandListener(this);
 // write post-init user code here
        }
        return formInserir;
    }
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: campo1 ">
    /**
     * Returns an initiliazed instance of campo1 component.
     * @return the initialized component instance
     */
    public TextField getCampo1() {
        if (campo1 == null) {
 // write pre-init user code here
            campo1 = new TextField("Luz", null, 32, TextField.ANY);
 // write post-init user code here
        }
        return campo1;
    }
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: campo2 ">
    /**
     * Returns an initiliazed instance of campo2 component.
     * @return the initialized component instance
     */
    public TextField getCampo2() {
        if (campo2 == null) {
 // write pre-init user code here
            campo2 = new TextField("Agua", null, 32, TextField.ANY);
 // write post-init user code here
        }
        return campo2;
    }
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: campo3 ">
    /**
     * Returns an initiliazed instance of campo3 component.
     * @return the initialized component instance
     */
    public TextField getCampo3() {
        if (campo3 == null) {
 // write pre-init user code here
            campo3 = new TextField("Cartao", null, 32, TextField.ANY);
 // write post-init user code here
        }
        return campo3;
    }
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {
 // write pre-action user code here
        if (displayable == alert) {
            if (command == okCommand1) {
 // write pre-action user code here
                switchDisplayable(null, getFormInserir());
 // write post-action user code here
            }
        } else if (displayable == formInserir) {
            if (command == exitCommand) {
 // write pre-action user code here
                exitMIDlet();
 // write post-action user code here
            } else if (command == okCommand) {
 // write pre-action user code here
                switchDisplayable(null, getAlert());
 // write post-action user code here
            }
        }
 // write post-action user code here
//Aqui a parte da conversao
String c1;
c1 = getCampo1().getString();
n1 = (Double.parseDouble(c1));

String c2;
c2 = getCampo2().getString();
n2 = (Double.parseDouble(c2));

String c3; 
c3 = getCampo3().getString();
//float valorExtra = (Float.parseFloat(vlrExtra)); 
n3 = (Double.parseDouble(c3));

double res = n1 + n2 + n3;
String str = Double.toString(res);
System.out.println(str);

    }
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: alert ">
    /**
     * Returns an initiliazed instance of alert component.
     * @return the initialized component instance
     */
    public Alert getAlert() {
        if (alert == null) {
 // write pre-init user code here
            alert = new Alert("Total", "Calculo OK", null, null);
            alert.addCommand(getOkCommand1());
            alert.setCommandListener(this);
            alert.setTimeout(Alert.FOREVER);
 // write post-init user code here
campo1.setString(" ");
campo2.setString(" ");
campo3.setString(" ");
        }
        return alert;
    }
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {
 // write pre-init user code here
            okCommand = new Command("Calcular", Command.OK, 0);
 // write post-init user code here
        }
        return okCommand;
    }
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {
 // write pre-init user code here
            exitCommand = new Command("Sair", Command.EXIT, 0);
 // write post-init user code here
        }
        return exitCommand;
    }
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">
    /**
     * Returns an initiliazed instance of okCommand1 component.
     * @return the initialized component instance
     */
    public Command getOkCommand1() {
        if (okCommand1 == null) {
 // write pre-init user code here
            okCommand1 = new Command("Salvo", Command.OK, 0);
 // write post-init user code here
        }
        return okCommand1;
    }
//</editor-fold>

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
}

e o erro que dá é esse:

[quote]
TRACE: <at java.lang.NumberFormatException>, Exception caught in Display class
java.lang.NumberFormatException
at java.lang.FloatingDecimal.readJavaFormatString(), bci=24
at java.lang.Double.parseDouble(Double.java:510)
at principal.commandAction(principal.java:179)
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.keyInput(), bci=436
at com.sun.midp.chameleon.CWindow.keyInput(), bci=38
at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleKeyEvent(), bci=17
at com.sun.midp.lcdui.DisplayEventListener.process(), bci=277
at com.sun.midp.events.EventQueue.run(), bci=179
at java.lang.Thread.run(Thread.java:662)
TRACE: <at java.lang.NumberFormatException>, Exception caught in Display class
java.lang.NumberFormatException
at java.lang.FloatingDecimal.readJavaFormatString(), bci=24
at java.lang.Double.parseDouble(Double.java:510)
at principal.commandAction(principal.java:179)
at javax.microedition.lcdui.Alert$1.commandAction(), bci=78
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.soft1(), bci=37
at com.sun.midp.chameleon.layers.SoftButtonLayer.keyInput(), bci=36
at com.sun.midp.chameleon.CWindow.keyInput(), bci=38
at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleKeyEvent(), bci=17
at com.sun.midp.lcdui.DisplayEventListener.process(), bci=277
at com.sun.midp.events.EventQueue.run(), bci=179
at java.lang.Thread.run(Thread.java:662)
TRACE: <at java.lang.NumberFormatException>, Exception caught in Display class
java.lang.NumberFormatException
at java.lang.FloatingDecimal.readJavaFormatString(), bci=24
at java.lang.Double.parseDouble(Double.java:510)
at principal.commandAction(principal.java:179)
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.soft1(), bci=37
at com.sun.midp.chameleon.layers.SoftButtonLayer.keyInput(), bci=36
at com.sun.midp.chameleon.CWindow.keyInput(), bci=38
at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleKeyEvent(), bci=17
at com.sun.midp.lcdui.DisplayEventListener.process(), bci=277
at com.sun.midp.events.EventQueue.run(), bci=179
at java.lang.Thread.run(Thread.java:662)[/quote]
O que será que pode ser? já que ele nem chega a mostrar no console do netbeans (mas se colocar essa conversão num arquivo java simples - J2SDK - vai!)…
Obrigado desde já.
Ricardo

Olá,

As conversões de n1 e n2 parecem passar, qual o valor de getCampo3().getString() quando o erro ocorre? Está usando o separador de decimais correto (ponto) no formulário?

[ ]'s

mesmo passando assim “1.0” ou passando “1” da erro, aquele que postei… obrigado desde já AINDA estou empacado nisso…
abraços
ricardo

Olá,

Tente converter o valor direto do TextField, assim:

n3 = Double.parseDouble(campo3.getString());

Poste os resultados.

[ ]'s