JcomboBox returns Null Pointer Exception

1

I'm doing a project to send packages in Java and I'm having a problem loading a combobox with data brought from a database. In the stack trace I am returning as error what can be seen in the image, and according to what I observed the line 239 that is referenced is when I try to add an element in the combobox, which returns me as a result Null . Of course, whoever is kind enough to help me will be grateful. Greetings

Here is my code to fill the combobox, I have it in a separate class called UsuarioDAO

    public void usuarios_combo(JComboBox cbox_usuarios){
    try {

        String sql = "SELECT usu_ci FROM usuarios";
        Connection conn = this.getConexion();
        PreparedStatement pst1 = conn.prepareStatement(sql);
        pst1.setQueryTimeout(5);
        ResultSet rs = pst1.executeQuery();

        while ((rs != null) && (rs.next())) {

            String ci = rs.getString(1);
            cbox_usuarios.addItem(ci);
        }

        pst1.close();
        rs.close();
        conn.close();

    }
    catch (Exception e) {
        System.err.println("Names : " + e.getClass().getName() + ": " + e.getMessage());
        e.printStackTrace();
    }        

}

Then in the presentation (the form where I show the parcels) I have the following:

public class ListarEncomiendasAdmin extends JFrame {

    private JPanel contentPane;
    private JTextField txtEstado;
    private JTextField txtOrigen;
    private JTextField txtDestino;
    private static JTable tblEncomiendas;
    private static EncomiendasDAO eDAO = new EncomiendasDAO();
    private static Object[][] dtEncomienda;
    private static JComboBox cmbRemitente;
    private static JComboBox cmbDestinatario;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                UsuarioDAO uDAO = new UsuarioDAO(); 
                try {
                    ListarEncomiendasAdmin frame = new ListarEncomiendasAdmin();
                    frame.setVisible(true);
                    Actualizar_Tabla();
                    uDAO.usuarios_combo(cmbRemitente);
                    uDAO.usuarios_combo(cmbDestinatario);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
asked by Jose Ignacio Radesca 04.09.2018 в 05:16
source

1 answer

0

Apparently I have seen your error in your code. The error you want to tell you is that you are not instantiating the cmbRemittent object, nor the cmbUsers object. Therefore the combo box is never initialized so it can not load the elements you want to add.

I recommend that you return a JComboBox as follows:

public JComboBox usuarios_combo(){
JComboBox cbox_usuarios = new JComboBox();
try {

    String sql = "SELECT usu_ci FROM usuarios";
    Connection conn = this.getConexion();
    PreparedStatement pst1 = conn.prepareStatement(sql);
    pst1.setQueryTimeout(5);
    ResultSet rs = pst1.executeQuery();

    while ((rs != null) && (rs.next())) {

        String ci = rs.getString(1);
        cbox_usuarios.addItem(ci);
    }

    pst1.close();
    rs.close();
    conn.close();

}
catch (Exception e) {
    System.err.println("Names : " + e.getClass().getName() + ": " + e.getMessage());
    e.printStackTrace();
}        
return cbox_usuarios;

Now from your class ListCommandAdmin you should match your comboBox to the method you modified. This in the following way:

public class ListarEncomiendasAdmin extends JFrame {

private JPanel contentPane;
private JTextField txtEstado;
private JTextField txtOrigen;
private JTextField txtDestino;
private static JTable tblEncomiendas;
private static EncomiendasDAO eDAO = new EncomiendasDAO();
private static Object[][] dtEncomienda;
private static JComboBox cmbRemitente;
private static JComboBox cmbDestinatario;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            UsuarioDAO uDAO = new UsuarioDAO(); 
            try {
                cmbRemitente = new JComboBox();
                cmbDestinatario = new JComboBox();
                ListarEncomiendasAdmin frame = new ListarEncomiendasAdmin();
                frame.setVisible(true);
                Actualizar_Tabla();
                cmbRemitente = uDAO.usuarios_combo();
                cmbDestinatario = uDAO.usuarios_combo();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

This way you have already initialized your ComnoBox, once the equalizations have been initialized to the cbox_usuarios (method that you send to call), in this way the items from cbox_usuarios to cmbRemittent and cmbDestinatario will be equalized. I hope it worked for you. Greetings.

    
answered by 04.09.2018 в 09:33