Null pointer exception when filling a JcomboBox with objects (object classes generated with hibernate)

0

At the time of filling a JcomboBox with an object that I bring directly from the class generated with hibernate I jump a null pointer exception , the generated classes are fine (I have looked a thousand times)

package InterfazPrueba;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import generadas.Fabricante;
import generadas.Modelo;

import javax.swing.JLabel;
import javax.swing.JComboBox;

public class PruebaCombo extends JFrame {

    private JPanel contentPane;
    private List<Fabricante> lista;
    public SessionFactory sesion = null;
    public Session session = null;
    private JComboBox comboBox;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    PruebaCombo frame = new PruebaCombo();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public PruebaCombo() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblModelos = new JLabel("Modelos");
        lblModelos.setBounds(71, 93, 46, 14);
        contentPane.add(lblModelos);

        comboBox = new JComboBox();
        comboBox.setBounds(127, 89, 114, 22);
        contentPane.add(comboBox);
        rellenarComboBox();
    }

    public void rellenarComboBox() {
        Query query = session.createQuery("from Fabricante");
        lista = query.list();
        for (Fabricante eq : lista) {
            comboBox.addItem(eq);
        }
    }
}

If you want the classes generated by hibernate I can also pass them, but for now I leave this.

    
asked by Adans 25.04.2017 в 14:02
source

1 answer

1

The problem is with the variable session that you use without having initialized it.

Query query = session.createQuery("from Fabricante"); 
    
answered by 25.04.2017 в 15:47