英文:
Why when passing an object between classes it becomes null?
问题
我正在开发一个Java应用程序。
我已经为创建对象创建了一个GUI,但我希望开始构建对象,然后将其传递到另一个GUI以完成构建,然后再将其存储在数据库中。
这是我称之为"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");
}
}
然后我创建了对象并设置了一些属性,然后将其发送到我称之为"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();
}
...
当我调试应用程序时,在将对象发送到另一个类之前和之后添加了断点。
在发送对象之前,它已经填充了信息并创建了对象。当我到达下一个断点时:
this.comprobanteOperaciones = comprobante;
但是在那里评估对象时,它为null。
英文:
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.
答案1
得分: 1
中断的代码行尚未执行,因此赋值尚未发生。在赋值语句下方添加一些代码,并在那里设置断点。
英文:
Breakpointed line is not executed yet, so the assignment is not happened. Add some code to below of the assigment and add breakpoint there
答案2
得分: 1
所以我通过在对象初始化后调用构造函数中填充信息的方法来修复了它:
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();
}
}
);
}
...
鉴于抽象父类中的 super() 方法调用了 initialize(),所以我尝试向尚未初始化且为 null 的对象中填充数据。这为我修复了这个问题。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论