Foros del Web » Programación para mayores de 30 ;) » Java »

llamada a metodo de otra clase

Estas en el tema de llamada a metodo de otra clase en el foro de Java en Foros del Web. Hola, soy novata con java, y tengo un error al hacer una llamada a unmetodo de otra clase. Es un ejercicio muy sencillo, una clase ...
  #1 (permalink)  
Antiguo 13/08/2008, 04:32
 
Fecha de Ingreso: agosto-2008
Mensajes: 19
Antigüedad: 15 años, 8 meses
Puntos: 0
llamada a metodo de otra clase

Hola, soy novata con java, y tengo un error al hacer una llamada a unmetodo de otra clase.


Es un ejercicio muy sencillo, una clase de tipo JFRAME tiene unbotón que hace una llamada al metodo HOLA de otra clase de tipo JPANEL (este panel no esta incluido en el jframe).

Estas son mis dos clases java:

---------------------------------------------
import javax.swing.JPanel;
public class panel extends JPanel {
public void hola(){
System.out.println("hola");
}
}
----------------------------------------------

Y esta es la clase que hace la llamada a la clase anterior:
------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class ventana extends JFrame {
public panel the_panel;
public static void main(String args[]) {
ventana frame = new ventana();
frame.setVisible(true);
}

public ventana() {
super();
getContentPane().setLayout(null);
final JButton button = new JButton();

button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
try{ the_panel.hola(); }
catch(Exception e){
System.err.println("Errorrrrr :" +e.toString());
e.printStackTrace();}
}
});
button.setBounds(104, 189, 106, 26);
getContentPane().add(button);
}
}



Sin embargo al hacer click en el botón obtengo el siguiente error:
---------------------
Errorrrrr :java.lang.NullPointerException
java.lang.NullPointerException
at ventana$1.actionPerformed(ventana.java:24)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown 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.mouseRe leased(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(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn 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.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
-----------------------------------------------------
  #2 (permalink)  
Antiguo 13/08/2008, 06:38
Avatar de HenrydeSousa  
Fecha de Ingreso: septiembre-2004
Ubicación: Venezuela
Mensajes: 300
Antigüedad: 19 años, 7 meses
Puntos: 2
Respuesta: llamada a metodo de otra clase

Prueba con

panel the_panel = new panel();
  #3 (permalink)  
Antiguo 13/08/2008, 07:36
Avatar de Llave  
Fecha de Ingreso: abril-2005
Ubicación: Galicia
Mensajes: 52
Antigüedad: 19 años
Puntos: 1
Respuesta: llamada a metodo de otra clase

Por extender un poco la solución que te ha dado HenrydeSousa.

Donde inicializas la variable the_panel, en vez de dejarla sin definir como has hecho tú.

Y esta es la clase que hace la llamada a la clase anterior:
------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class ventana extends JFrame {
public panel the_panel;
public static void main(String args[]) {
ventana frame = new ventana();
frame.setVisible(true);
}

Prueba a inicializarla como te aconseja Henry:

public panel the_panel = newpanel();

Cuéntanos como te fue.
  #4 (permalink)  
Antiguo 16/08/2008, 02:02
 
Fecha de Ingreso: agosto-2008
Mensajes: 19
Antigüedad: 15 años, 8 meses
Puntos: 0
Respuesta: llamada a metodo de otra clase

Muchísimas gracias , ¡ ahora funciona !

perdonad la pregunta tan básica pero esque me he visto envuelta en un proyecto java y me he saltado pasos previos como conocer la teoría , y realizar ejercicios más básicos.

Muchas gracias y un saludo.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:47.