Java - ayuda pasar objeto por referencia

 
Vista:

ayuda pasar objeto por referencia

Publicado por geronimo (1 intervención) el 14/11/2010 23:56:06
NO ME LLEGAN POR MAIL, ASI QUE LO HAGO DENUEVO..

hola gente, tengo un problema con el paso por referencia en java!
el tema es asi!

tengo un JDialog donde:

public class JDialog implements JActionListener{
JTextField edit1, edit2,.....
JLabel label1,label2,.....
JButtom boton!

Vector<Persona> lista ; //ACA EL TEMA

public JDialog (Vector<Persona> lista){

boton = new JButtom("aceptar");
boton.addActionListener(this);
//agrego botones y labels y demas

/*el tema es que quiero que cuando clieckee en el boton el evento cree una persona y la agregue a esta lista que pase como paramentro ... lo que hago es esto*/

this.lista = lista; // ACA LA PIFIADA

}

/* este es mi actionperformed*/
public void actionPerformed(ActionEvent arg0) {

Persona p = new Persona();
p.setApellido(eApellido.getText());
p.setNombre(eNombre.getText());
p.setEdad(Integer.parseInt(eEdad.getText()));
p.setDireccion(eDireccion.getText());
this.lista.add(p); // o lista.add(p) /*AERROR ERROR*/

this.dispose();
}

}

Es como que no puedo copiar una referencia que llego en el metodo....no entiendo, es un tema teorico parece...pero no entiendo bien cono funciona esto de pasar copias de referencias!

Digo q el error esta ahi porq si creo persona en el constructor y le pongo valores...se hace bien!
el tema es q quiero poder leerlos de los edit y para eso tiene q ser cuando apriete el boton

SIN MAS

LA EXCEPTION:

Exception occurred during event dispatching:
java.lang.NullPointerException
at VentanaSecundaria.actionPerformed(VentanaSecundaria.java:74)
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.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.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.Dialog$1.run(Unknown Source)
at java.awt.Dialog$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Dialog.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at java.awt.Dialog.setVisible(Unknown Source)
at VentanaSecundaria.<init>(VentanaSecundaria.java:58)
at VentanaPrincipal.actionPerformed(VentanaPrincipal.java:46)
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.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.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)

ESPERO QUE ME SEPAN EXPLICAR PORQUE PASA ESTO, MAS QUE DARME LA SOLUCION

un saludo ...
y gracias desde ya
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:ayuda pasar objeto por referencia

Publicado por Javier (41 intervenciones) el 15/11/2010 00:45:46
Hola,

por lo que puedo ver, no hay error, tenes que fijarte que la lista que le hayas mandado por parametro haya sido previamente creada, sino te tira ese error,

crea la lista asi: Vector<Persona> lista= new Vector<Persona>(); y la pasas por parametro al JDialog. Si la variable no esta instanciada, te tira el error que te aparece a vos.

Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:ayuda pasar objeto por referencia

Publicado por geronimo (20 intervenciones) el 15/11/2010 01:27:16
Hola gracias por responder!
si, el objeto lo instancie antes en la clase principal!
el problema era ahi...cuando intentaba guardar la referencia que llegaba en el constructor, en vez de usarla dentro del metodo! (nose si me entendes)
despues es como que no se referia a lo mismo y me tiraba toda esa chorrera de ecepciones

la verdad que no se porque pasaba!
lo que tuve que hacer en el actionperformed del JDialog solamente esconder la ventana!

y desde la clase principal lei los edits de el JDialog y despues destrui el Jdialog!

Es la solucion que encontren, no se si es la mejor, pero bue!!

Gracias, un saludo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

ayuda pasar objeto por referencia

Publicado por Matias (1 intervención) el 07/03/2012 06:54:37
EL Action Perform deberia ir dentro del actionListener del boton de esta forma, por lo menos a mi me funciona bien asi:
btnCrearCliente.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent e) {
DtoCliente dtoCli = new DtoCliente(txtNombreCliente.getText(), txtDireccion.getText(),txtPrioridad.getText());
try {
Fachada.guardarCliente(dtoCli);
} catch (Exception ex) {
lblError.setText(ex.getMessage());
lblError.setVisible(true);
}

}
});
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar