Why when passing an object between classes it becomes null?

Viewed 79

I am developing a Java application.

I have created a GUI for creating an object, but I want to begin constructing it, then pass it to other GUI to finish the construction, before storing it in the database.

This is a class that I call the Selector:

    private void aceptar() throws Exception {
        Comprobante comprobanteOperaciones = new Comprobante();
        if (partidas.size() > 0) {
            comprobanteOperaciones.setPartidas(partidas);
            comprobanteOperaciones.setFechaOperaciones(dialog.getTxtFechaOperaciones().getDate());
            comprobanteOperaciones.setDescripcion(generarDescripcion());
            new ComprobanteOperacionesRegistrarController(dialog, securityFacade, comprobanteOperaciones).openDialog();
        } else {
            throw new Exception("No hay partidas seleccionadas");
        }
    }

Then I create the object and I set some of the attributes, before sending it to the other class, that I call Register:

    public class ComprobanteOperacionesRegistrarController{

    private Comprobante comprobanteOperaciones;

    public ComprobanteOperacionesRegistrarController(Window window,
                                                     SeguridadFacade seguridadFacade, Comprobante comprobante) throws Exception {
        super(new UI_ComprobanteOperacionesRegistrar(window), seguridadFacade);
        this.comprobanteOperaciones = comprobante;
    }

    @Override
    protected void initialize() throws Exception {
        operacionesFacade = OperacionesFacade.getInstance();
        configuracionFacade = ConfiguracionFacade.getInstance();
        reportesFacade = ReportesFacade.getInstance();
        tableOperacionesModel = (DefaultTableModel) dialog.getTableOperaciones().getModel();

        dialog.getBtnCancelar().addActionListener(this);
        dialog.getBtnImprimir().addActionListener(this);
        dialog.getTxtFechaOperaciones().addPropertyChangeListener(
                new PropertyChangeListener() {
                    @Override
                    public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
                        llenarPartidasParcial();
                    }
                }
        );

        llenarNoComprobante();
        llenarDatosComprobante();
        llenarPartidasParcial();
    }
...

When I debug the app, I added breakpoints before and after sending the object to the other class.

Before sending the object, it is filled with the information and the object is created. When I go to the next breakpoint:

this.comprobanteOperaciones = comprobante;

But when I evaluate the object there, it's null.

2 Answers

Breakpointed line is not executed yet, so the assignment is not happened. Add some code to below of the assigment and add breakpoint there

So I fixed it by calling the methods that fill information in the constructor, after the object was initialized:

public class ComprobanteOperacionesRegistrarController{

    private Comprobante comprobanteOperaciones;

    public ComprobanteOperacionesRegistrarController(Window window,
                                                     SeguridadFacade seguridadFacade, Comprobante comprobante) throws Exception {
        super(new UI_ComprobanteOperacionesRegistrar(window), seguridadFacade);
        this.comprobanteOperaciones = comprobante;

        llenarNoComprobante();
        llenarDatosComprobante();
        llenarPartidasParcial();
    }

    @Override
    protected void initialize() throws Exception {
        operacionesFacade = OperacionesFacade.getInstance();
        configuracionFacade = ConfiguracionFacade.getInstance();
        reportesFacade = ReportesFacade.getInstance();
        tableOperacionesModel = (DefaultTableModel) dialog.getTableOperaciones().getModel();

        dialog.getBtnCancelar().addActionListener(this);
        dialog.getBtnImprimir().addActionListener(this);
        dialog.getTxtFechaOperaciones().addPropertyChangeListener(
                new PropertyChangeListener() {
                    @Override
                    public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
                        llenarPartidasParcial();
                    }
                }
        );
    }
...

Given that super() method in the Abstract parent class, calls the initialize(), so I was trying to fill up data into an object that was not initialized and was null yet. This fixed it for me.

Related