How to return the value of a sql database combobox in a Jtextfield?

Asked

Viewed 57 times

0

In my program I have a register of customers in which contain two combobox.
One is districts and the other is counties.

As I show in the next photo in the comboDistricts has several districts of Portugal.

When selecting Porto or any other district will show in the comboConcelho the counties corresponding to these districts. So the comboConcelho depends on the comboDistrito.

For example, if you choose the "Porto" list in the municipalities it will show all the municipalities of Porto as shown in the following image:

inserir a descrição da imagem aqui

When registering a client in the database, the district and county fields show the ID corresponding to each of them. In the following photo I show an example of a registered customer with district PORTO and county MAIA

inserir a descrição da imagem aqui

Problem is when I try to find this client, in this case loading the client into the corresponding Jtextfield returns me an error. In the next photo shows to load all fields except combos.inserir a descrição da imagem aqui

The error that returns me is as follows:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Programa.M_Clientes$2.itemStateChanged(M_Clientes.java:444) at javax.swing.JComboBox.fireItemStateChanged(Unknown Source) at javax.swing.JComboBox.selectedItemChanged(Unknown Source) at javax.swing.JComboBox.contentsChanged(Unknown Source) at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source) at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source) at javax.swing.JComboBox.setSelectedItem(Unknown Source) at javax.swing.JComboBox.setSelectedIndex(Unknown Source) at Programa.M_Clientes.procura_contribuinte_e_coloca(M_Clientes.java:382) at Programa.M_Clientes$10.actionPerformed(M_Clientes.java:814) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

The photo error is also available here for those who prefer:

inserir a descrição da imagem aqui

In the first line of error "M_clients.java.444" when clicking directs me to this code: inserir a descrição da imagem aqui

Code of the photo here:

comboModel1 = new DefaultComboBoxModel();
    comboModel1 = carrega_combo_distrito();
    comboBoxDistrito = new JComboBox(comboModel1);
    comboBoxDistrito.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent arg0) {

            comboModel2 = new DefaultComboBoxModel();
            QueryResultDistritos temp = (QueryResultDistritos) comboBoxDistrito.getSelectedItem();
            comboModel2 = carrega_combo_concelho(temp.recordCode);
            comboBoxConcelhos.setModel(comboModel2);


        }
    });

Another error line that says "M_clients.java:382" if you click directs me to this code: inserir a descrição da imagem aqui

Code of the photo here:

void procura_contribuinte_e_coloca(){



        ClientesLigaBD cl = new ClientesLigaBD();
        if((cl.verifica_se_existe(txt_contribuinte.getText()))){

            Clientes lido = new Clientes();

            lido = cl.LerRegisto(txt_contribuinte.getText());

            txt_nome.setText(lido.getNome_cliente());
            txt_n_cliente.setText(String.valueOf(lido.getN_cliente()));
            txt_rua_av.setText(lido.getRua_av());
            txt_n_rua_av.setText(String.valueOf(lido.getN_rua_av()));
            txt_local.setText(lido.getLocal().trim());
            txt_email.setText(lido.getEmail());
            txt_telefone.setText(String.valueOf(lido.getTelefone()));
            txt_telemovel.setText(String.valueOf(lido.getTelemovel()));
            txt_cod_postal.setText(lido.getCod_postal());

            int idxD = getComboIndexD(comboBoxDistrito, lido.getDistrito());
            comboBoxDistrito.setSelectedIndex(idxD);

            int idxC = getComboIndexC(comboBoxConcelhos, lido.getConcelho());
            comboBoxConcelhos.setSelectedIndex(idxC);






            JOptionPane.showMessageDialog(null, "Cliente encontrado !");


        }else{

            JOptionPane.showMessageDialog(null, "Não existe nenhum cliente com esse número contribuinte.");

        }
    }

This code carries clients in Jtextfields and combos.

Can anyone tell me what’s wrong with loading the combos?

  • Provide a [mcve] because swing needs to be tested and run to detect the problem.

  • Problem is this is a very big project and I don’t know how I’m going to put it here

  • You don’t have to put the whole project. That’s why it’s called example MINIMUM, complete and verifiable, access the link and read the instructions on how to make a.

  • OK I’ll try to make the example MINIMO

  • Ricardo, see the question I marked as duplicate, what Oce really wants is to capture the selected item and fill the text field from the selection change, in the linked question has an answer of mine that shows how to do this.

  • Where is the link with the question? I can’t find

  • Under the title

  • I’m afraid that resolution didn’t solve the problem

  • Then provide a [mcve] as I have commented before.

  • But the program contains data base as I will include it here ?

  • Did you even bother to click the link and read it all the way through? On the tips page for creating a minimal, complete and verifiable example, just follow them.

Show 6 more comments
No answers

Browser other questions tagged

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