Setar dados de um BD em JTextField utilizando JComboBox

11 respostas
programaçãojavamysql
B

Boa tarde.

Eu tenho um JComboBox que lista itens de uma tabela do Mysql, e ao selecionar o item deste combobox, preciso que seus dados sejam setados em texts fields. Mas ele me apresenta o erro “java.lang.NullPointerException”. Utilizo o código abaixo:

private void jcbProdutosActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
        try {
            String query = "SELECT * FROM produtos WHERE nome = ?"; 
            PreparedStatement pst = conn.prepareStatement(query);
            pst.setString(1, (String)jcbProdutos.getSelectedItem());                
            ResultSet rs = pst.executeQuery();                
            while(rs.next()){
                tfCodProd.setText(rs.getString("codigo"));
                tfValorUn.setText(rs.getString("nome"));
            }
            pst.close();
        } catch (Exception e) {
            e.printStackTrace();
        }  
}

11 Respostas

B

Ninguém mesmo? =/

o.oliveira

Faz um tempinho que não mexo com java mas ainda tenho alguns trabalhinhos assim que fiz em sala, vou ver se consigo ajudar…

Em qual linha ele está apontando erro de NullPointer?

B

Coloquei o erro completo aí. Mas deu pra entender o que eu quero fazer? Estou no caminho certo?

run:
java.lang.NullPointerException
	at view.ViewVenda.jcbProdutosActionPerformed(ViewVenda.java:679)
	at view.ViewVenda.access$600(ViewVenda.java:32)
	at view.ViewVenda$8.actionPerformed(ViewVenda.java:289)
	at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1258)
	at javax.swing.JComboBox.contentsChanged(JComboBox.java:1332)
	at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
	at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93)
	at javax.swing.DefaultComboBoxModel.addElement(DefaultComboBoxModel.java:131)
	at javax.swing.JComboBox.addItem(JComboBox.java:716)
	at view.ViewVenda.listarProdutos(ViewVenda.java:72)
	at view.ViewVenda.<init>(ViewVenda.java:60)
	at view.ViewVenda$17.run(ViewVenda.java:895)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
	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:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	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)
java.lang.NullPointerException
	at view.ViewVenda.jcbProdutosActionPerformed(ViewVenda.java:679)
	at view.ViewVenda.access$600(ViewVenda.java:32)
	at view.ViewVenda$8.actionPerformed(ViewVenda.java:289)
	at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1258)
	at javax.swing.JComboBox.setSelectedItem(JComboBox.java:586)
	at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:622)
	at view.ViewVenda.desabilitarCampos(ViewVenda.java:497)
	at view.ViewVenda.<init>(ViewVenda.java:64)
	at view.ViewVenda$17.run(ViewVenda.java:895)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
	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:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	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)
java.lang.NullPointerException
	at view.ViewVenda.jcbProdutosActionPerformed(ViewVenda.java:679)
	at view.ViewVenda.access$600(ViewVenda.java:32)
	at view.ViewVenda$8.actionPerformed(ViewVenda.java:289)
	at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1258)
	at javax.swing.JComboBox.contentsChanged(JComboBox.java:1332)
	at javax.swing.JComboBox.intervalRemoved(JComboBox.java:1352)
	at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:179)
	at javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:174)
	at javax.swing.JComboBox.removeAllItems(JComboBox.java:771)
	at view.ViewVenda.listarProdutos(ViewVenda.java:70)
	at view.ViewVenda.novoProduto(ViewVenda.java:810)
	at view.ViewVenda.jbNovaVendaActionPerformed(ViewVenda.java:615)
	at view.ViewVenda.access$400(ViewVenda.java:32)
	at view.ViewVenda$5.actionPerformed(ViewVenda.java:226)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
	at java.awt.Component.processMouseEvent(Component.java:6533)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
	at java.awt.Component.processEvent(Component.java:6298)
	at java.awt.Container.processEvent(Container.java:2236)
	at java.awt.Component.dispatchEventImpl(Component.java:4889)
	at java.awt.Container.dispatchEventImpl(Container.java:2294)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
	at java.awt.Container.dispatchEventImpl(Container.java:2280)
	at java.awt.Window.dispatchEventImpl(Window.java:2746)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	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:76)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.awt.EventQueue$4.run(EventQueue.java:731)
	at java.awt.EventQueue$4.run(EventQueue.java:729)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	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)
java.lang.NullPointerException
	at view.ViewVenda.jcbProdutosActionPerformed(ViewVenda.java:679)
	at view.ViewVenda.access$600(ViewVenda.java:32)
	at view.ViewVenda$8.actionPerformed(ViewVenda.java:289)
	at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1258)
	at javax.swing.JComboBox.contentsChanged(JComboBox.java:1332)
	at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
	at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93)
	at javax.swing.DefaultComboBoxModel.addElement(DefaultComboBoxModel.java:131)
	at javax.swing.JComboBox.addItem(JComboBox.java:716)
	at view.ViewVenda.listarProdutos(ViewVenda.java:72)
	at view.ViewVenda.novoProduto(ViewVenda.java:810)
	at view.ViewVenda.jbNovaVendaActionPerformed(ViewVenda.java:615)
	at view.ViewVenda.access$400(ViewVenda.java:32)
	at view.ViewVenda$5.actionPerformed(ViewVenda.java:226)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
	at java.awt.Component.processMouseEvent(Component.java:6533)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
	at java.awt.Component.processEvent(Component.java:6298)
	at java.awt.Container.processEvent(Container.java:2236)
	at java.awt.Component.dispatchEventImpl(Component.java:4889)
	at java.awt.Container.dispatchEventImpl(Container.java:2294)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
	at java.awt.Container.dispatchEventImpl(Container.java:2280)
	at java.awt.Window.dispatchEventImpl(Window.java:2746)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	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:76)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.awt.EventQueue$4.run(EventQueue.java:731)
	at java.awt.EventQueue$4.run(EventQueue.java:729)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	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)
java.lang.NullPointerException
	at view.ViewVenda.jcbProdutosActionPerformed(ViewVenda.java:679)
	at view.ViewVenda.access$600(ViewVenda.java:32)
	at view.ViewVenda$8.actionPerformed(ViewVenda.java:289)
	at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1258)
	at javax.swing.JComboBox.setSelectedItem(JComboBox.java:586)
	at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:622)
	at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:852)
	at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
	at java.awt.Component.processMouseEvent(Component.java:6533)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
	at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:501)
	at java.awt.Component.processEvent(Component.java:6298)
	at java.awt.Container.processEvent(Container.java:2236)
	at java.awt.Component.dispatchEventImpl(Component.java:4889)
	at java.awt.Container.dispatchEventImpl(Container.java:2294)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
	at java.awt.Container.dispatchEventImpl(Container.java:2280)
	at java.awt.Window.dispatchEventImpl(Window.java:2746)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	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:76)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.awt.EventQueue$4.run(EventQueue.java:731)
	at java.awt.EventQueue$4.run(EventQueue.java:729)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	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)
CONSTRUÍDO COM SUCESSO (tempo total: 17 segundos)
o.oliveira

Bruno,

puxei meus códigos e achei o seguinte:

public boolean recuperarTudo(Conexao c, LinkedList<Endereco> listaUFs) {
    try {
                Statement st;
                st = c.getCon().createStatement();
                ResultSet rs = st.executeQuery("SELECT * FROM estado");      
                if(rs!=null){
                    while(rs.next()){
                        Endereco endereco = new Endereco();
                        endereco.setUf(rs.getString("uf"));
                        listaUFs.add(endereco);
                    }
                }
                return true;
            } catch (SQLException e) {
                e.printStackTrace();
            }
          return false;
    }

Veja que é necessário um teste de if(rs!=null) pois o retorno daquela Query pode ser vazia, dai estarás tentando acessar a função next() de um objeto nulo, resultando no erro que estás tendo.

Tenta colocar aquele IF e ve se vai dar certo.
Mantenha-me informado.
Abraço

ViniGodoy

O método executeQuery nunca retorna null. Não é necessário fazer esse if.

Qual dessas é a linha 679 do código? (Linha onde a exception aponta o erro)?

B

A linha 679 é esta:

PreparedStatement pst = conn.prepareStatement(query);

B

Com o “if” também não deu certo

felipe1234

Estou desenvolvendo um projeto estou querendo fazer isso tambem

felipe1234

mas esta dando erro

felipe1234

Se conseguir a resposat me avisa porfavor

ViniGodoy

Provavelmente a variável conn não foi inicializada.

Criado 14 de dezembro de 2016
Ultima resposta 14 de jan. de 2017
Respostas 11
Participantes 4