简体   繁体   中英

runtime error with a simple button handler program in java

Hello im having a problem with a basic buttons handlers program. I get this error when I run it.

Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1086)
    at java.awt.Container.add(Container.java:998)
    at javax.swing.JFrame.addImpl(JFrame.java:562)
    at java.awt.Container.add(Container.java:966)
    at practicagrafica5.botonpanel.<init>(botonpanel.java:44)
    at practicagrafica5.Practicagrafica5.main(Practicagrafica5.java:17)
Java Result: 1

heres mi code:

    package practicagrafica5;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class botonpanel {


    JButton botones[];
    String nombresbot[]={"primero","segundo","tercero"};
    JTextArea areatexto;
    JPanel panelbotones;
    JFrame ventana;

    botonpanel(){
    JFrame.setDefaultLookAndFeelDecorated(true);
    ventana= new JFrame("botones de prueba");
    ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ventana.setLayout(new BorderLayout());
     ventana.setSize(400, 400);

    /* NOTA:
     ventana.setIconImage(Toolkit.getDefaultToolKit().createImage("aqui iria la ruta de la imagen en la pc")
    */
    botones=new JButton[nombresbot.length];
    panelbotones=new JPanel();
    panelbotones.setLayout(new FlowLayout(FlowLayout.LEFT));


    for (int i=0; i<nombresbot.length; i++){
    botones[i]=new JButton();
    botones[i].setName(nombresbot[i]);
    botones[i].setToolTipText("este es "+nombresbot[i]);
    botones[i].addMouseListener(new handlerbotones());
    panelbotones.add(botones[i]);
    }
    ventana.add(panelbotones,BorderLayout.NORTH);
    ventana.add(areatexto,BorderLayout.CENTER);
    ventana.setVisible(true);
    }//finaliza el constructor

    class handlerbotones extends MouseAdapter{

        @Override
        public void mousePressed(MouseEvent e){
            //como JPanel es un "hijo" de Component utilizamos en los metodos un objeto de la clase Component
            //y con su metodo getComponent obtendremos una referencia al componente en este caso el boton.
    Component aux = e.getComponent();
    System.out.println(aux.getName());
    }
        @Override
    public void mouseReleased(MouseEvent e){
     Component aux= e.getComponent();
            switch (aux.getName()) {
                case "primero":
                    areatexto.append("se ha pulsado el primer boton \n");
                    break;
                case "segundo":
                    areatexto.append("se ha pulsado el segundo \n");
                    break;
                case "tercero":
                    areatexto.append("se ha pulsado el tercero \n");
                    break;
                default:
                    areatexto.append("no has pulsado un coño");
                    break;
            }
    }
    }//finaliza la clase interna handlerbotones


}

the code in the main application is just:

package practicagrafica5;

public class Practicagrafica5 {


    public static void main(String[] args) {
    botonpanel lol= new botonpanel();
    }
}

Please my head is going to blow with this. I dont know where ir my error. Any advice would help.

thank you.

你永远不会创建areatexto对象,例如

areatexto = new JTextArea();

The error is here: "ventana.add(areatexto,BorderLayout.CENTER)". The problem is that you're passing areatexto as parameter, but you've not yet created any object of type JTextArea. So before you have to assign an object of type JTextArea to areatexto. It would be something like this:

areatexto=new JTextArea("hola");

By the way, by convention the names of the classes should have the first letter in Capitals (Botonpanel, not botonpanel)

Good luck with your practica!

Toni.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM