Data conversion using Combobox

Asked

Viewed 40 times

-1

Hello, I’m having trouble dealing with an error in my program.

I have two combobox inside a registration panel. When executing already appears the list of products and customers, but the code field is not filling. I created a Popupmenuwillbecomeinvisible event on each combobox.

private void preencherCodigoClientePeloCombobox(){        
       
        modelCliente  = controllerCliente.getClienteController(jcbNomeCliente.getSelectedItem().toString());
        jtfCodigoCliente.setText(String.valueOf(modelCliente.getIdCliente()));
    }
    
    
    
    private void preencherCodigoProdutoPeloCombobox(){
     
            modelProdutos = controllerProdutos.retornarProdutoController(jcbNomeProduto.getSelectedItem().toString());
            jtfCodigoProduto.setText(String.valueOf(modelProdutos.getIdProduto()));      

    }

E o java pedi para fazer a conversão de String para inteiro, Fiz asssim:

private void preencherCodigoClientePeloCombobox(){        
       
        modelCliente  = controllerCliente.getClienteController(Integer.parseInt(jcbNomeCliente.getSelectedItem().toString()));
        jtfCodigoCliente.setText(String.valueOf(modelCliente.getIdCliente()));
    }
    
    
    
    private void preencherCodigoProdutoPeloCombobox(){
     
            modelProdutos = controllerProdutos.retornarProdutoController(Integer.parseInt(jcbNomeProduto.getSelectedItem().toString()));
            jtfCodigoProduto.setText(String.valueOf(modelProdutos.getIdProduto()));      

    }

Only it’s still wrong:

run:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "teste"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at view.ViewVendas.preencherCodigoClientePeloCombobox(ViewVendas.java:538)
    at view.ViewVendas.<init>(ViewVendas.java:42)
    at view.ViewVendas$11.run(ViewVendas.java:531)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Any possible solution?

1 answer

0

You have a NumberFormatException

This exception is being made when you convert String to the whole type int

To prevent this problem from happening you should make sure that the String you are converting can be transformed into int. Make sure your String has no spaces, special characters, letters, anything other than a number.

Note that you tried to convert the word "test" to int, That’s never gonna work because that word can’t be converted into a number. You should pass a string containing numbers only.

String stringDeNumeros = "12345";
Int conversao = Integer.parseInt(string DeNumeros);

It works.

But the code below does not:

String stringDeNumeros = "teste12345";
Int conversao = Integer.parseInt(string DeNumeros);
  • I have these methods that fill my combobox with my data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.