Erro no formulario: java.lang.NumberFormatException: For input string: ""?

Pessoal fiz um formulario pra cadastrar um cliente no netbeans fiz o código abaixo que está em itálico em azul e sempre dá um erro que relato abaixo.
Obs: o metodo “x.cadastra” recebe um objeto e coloca em um vetor.

package GUI;

import Objeto.Cliente;
import Objeto.Endereco;
import Operações.AcmeVideo;

public class Clientes extends javax.swing.JFrame {

/** Creates new form Clientes */
public Clientes() {
    initComponents();

    Cliente a=new Cliente();
    IDCliente.setText("000"+a.getIdCliente());
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    labelTitCli = new javax.swing.JLabel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    IDTitCli = new javax.swing.JLabel();
    IDCliente = new javax.swing.JLabel();
    buttonCadCli = new javax.swing.JButton();
    buttonSairCli = new javax.swing.JButton();
    labelTeste = new javax.swing.JLabel();
    separadorCli = new javax.swing.JSeparator();
    jLabel3 = new javax.swing.JLabel();
    labelRua = new javax.swing.JLabel();
    labelBairro = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    textNomeCli = new javax.swing.JTextField();
    textCPF = new javax.swing.JTextField();
    textRua = new javax.swing.JTextField();
    textBairro = new javax.swing.JTextField();
    textTel = new javax.swing.JTextField();
    textNro = new javax.swing.JTextField();

    setTitle("ACME-Cadastros de Clientes");

    labelTitCli.setFont(new java.awt.Font("Tahoma", 1, 12));
    labelTitCli.setText("Cadastro de Clientes");

    jLabel1.setText("Nome:");

    jLabel2.setText("CPF:");

    IDTitCli.setText("ID");

    IDCliente.setText("0000");

    buttonCadCli.setText("Cadastrar");
    buttonCadCli.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            buttonCadCliActionPerformed(evt);
        }
    });

    buttonSairCli.setText("Sair");
    buttonSairCli.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            buttonSairCliActionPerformed(evt);
        }
    });

    labelTeste.setText("jLabel3");

    jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
    jLabel3.setText("Endereço");

    labelRua.setText("Rua:");

    labelBairro.setText("Bairro:");

    jLabel4.setText("Tel:");

    jLabel5.setText("Nº:");

    textNomeCli.setText("jTextField1");

    textCPF.setText("jTextField1");

    textRua.setText("jTextField1");

    textBairro.setText("jTextField1");

    textTel.setText("jTextField1");

    textNro.setText("jTextField1");

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .addContainerGap()
            .add(jLabel3)
            .add(18, 18, 18)
            .add(separadorCli, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 530, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(18, Short.MAX_VALUE))
        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(labelBairro)
                        .add(layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                .add(labelRua)
                                .add(textRua, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)
                                .add(textBairro))
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 53, Short.MAX_VALUE)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(jLabel5)
                                .add(jLabel4)
                                .add(textTel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 176, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(textNro, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 91, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(textNomeCli)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, labelTitCli, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)
                        .add(layout.createSequentialGroup()
                            .add(IDTitCli)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(IDCliente))
                        .add(jLabel1))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 48, Short.MAX_VALUE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jLabel2)
                        .add(textCPF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 181, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(layout.createSequentialGroup()
                    .add(131, 131, 131)
                    .add(labelTeste, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 116, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 282, Short.MAX_VALUE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(buttonCadCli)
                        .add(buttonSairCli))))
            .add(22, 22, 22))
    );

    layout.linkSize(new java.awt.Component[] {buttonCadCli, buttonSairCli}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

    layout.setVerticalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .addContainerGap()
            .add(labelTitCli, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(IDTitCli)
                .add(IDCliente))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(jLabel1)
                .add(jLabel2))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(textNomeCli, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(textCPF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(1, 1, 1)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(32, 32, 32)
                    .add(separadorCli, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(layout.createSequentialGroup()
                    .add(18, 18, 18)
                    .add(jLabel3)))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                .add(layout.createSequentialGroup()
                    .add(labelRua)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(textRua, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(15, 15, 15)
                    .add(labelBairro)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(textBairro, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(layout.createSequentialGroup()
                    .add(jLabel5)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(textNro, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jLabel4)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(textTel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 64, Short.MAX_VALUE)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(buttonCadCli)
                .add(labelTeste))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(buttonSairCli)
            .add(27, 27, 27))
    );

    layout.linkSize(new java.awt.Component[] {buttonCadCli, buttonSairCli}, org.jdesktop.layout.GroupLayout.VERTICAL);

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width-640)/2, (screenSize.height-420)/2, 640, 420);
}// </editor-fold>

private void buttonCadCliActionPerformed(java.awt.event.ActionEvent evt) {                                             
    [color=darkblue]AcmeVideo x=new AcmeVideo();
    Cliente a=new Cliente();
    Endereco b=new Endereco();

    String id=IDCliente.getText().trim();
    a.setIdCliente(Integer.parseInt(id));
    

    String nome=textNomeCli.getText().trim();
    a.setNome(nome);
    

    String cpf=textCPF.getText().trim();
    a.setCpf(cpf);
    

    String rua=textRua.getText().trim();
    b.setRua(rua);
    
    
    String bairro=textBairro.getText().trim();
    b.setBairro(bairro);
    
    
    String nro=textNro.getText().trim();
    b.setNumero(nro);
    
    
    String tel=textTel.getText().trim();
    b.setTelefone(tel);
    
    
    x.cadastrar(a);

    IDCliente.setText("");
    textNomeCli.setText("");
    textCPF.setText("");
    textRua.setText("");
    textBairro.setText("");
    textNro.setText("");
    textTel.setText("");

[/color]

}                                            

private void buttonSairCliActionPerformed(java.awt.event.ActionEvent evt) {                                              
    System.exit(0);
}                                             

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Clientes().setVisible(true);
        }
    });
}

// Variables declaration - do not modify
private javax.swing.JLabel IDCliente;
private javax.swing.JLabel IDTitCli;
private javax.swing.JButton buttonCadCli;
private javax.swing.JButton buttonSairCli;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel labelBairro;
private javax.swing.JLabel labelRua;
private javax.swing.JLabel labelTeste;
private javax.swing.JLabel labelTitCli;
private javax.swing.JSeparator separadorCli;
private javax.swing.JTextField textBairro;
private javax.swing.JTextField textCPF;
private javax.swing.JTextField textNomeCli;
private javax.swing.JTextField textNro;
private javax.swing.JTextField textRua;
private javax.swing.JTextField textTel;
// End of variables declaration

}

Saída: A primeira vez da ok, mas na segunda da o erro:
[color=red]Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:468)
at java.lang.Integer.parseInt(Integer.java:497)
at GUI.Clientes.buttonCadCliActionPerformed(Clientes.java:231)
at GUI.Clientes.access$000(Clientes.java:22)
at GUI.Clientes$1.actionPerformed(Clientes.java:78)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)[/color]

Se alguma das caixas de texto que você usar para converter estiver vazia vai acontecer isso. Por isso você precisa verificar se há algo na caixa de texto antes de tentar converter.

PS: use as tags “code”!!!

1 curtida

Cara é vero acabei de ver este post link: http://www.guj.com.br/posts/list/73247.java
O pro é que é um trabalho um colega fez o código e eu faço o swing daí já vi que vou ter que analisar todo o código. Mas pra que serve esse " code "?
Vlw.

Sobre o “code” e outras coisas do fórum: http://guj.com.br/posts/list/50115.java

Porém a única String vazia como pode ver foi uma pra limpar o jtext, e ae como faço pra limpar vou ter que criar um método só pra isso?
Vou dar uma olhada no code, Vlw.

Para você esvaziar a caixa de texto é do jeito que está mesmo. O problema está aqui:

// olha para que servem as tags code!
String id=IDCliente.getText().trim();
a.setIdCliente(Integer.parseInt(id)); 

Se a caixa de texto “IDCliente” estiver vazia (ou com espaços somente) a variável “id” vai ser uma String vazia ("").
E ao tentar converter isso na linha seguinte, a exceção ocorre.

1 curtida

Muito bom mesmo, agora eu sei porque ainda estou no 3º periódo rs. Mas vou aprender a ter um olhar mais detalhado do código. Obg.

Aproveita também e aprende a usar as tags code aqui no guj.

  seu código aqui

Senão fica difícil mesmo ler seu código…