Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8870843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:50:11+00:00 2026-06-14T17:50:11+00:00

Hello im having a problem with a basic buttons handlers program. I get this

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:50:12+00:00Added an answer on June 14, 2026 at 5:50 pm

    You never create the areatexto object, e.g.

    areatexto = new JTextArea();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i am having a problem with resuming a thread my code is public
Hello I am new at Haskell and i'm having problems trying to get this
Hello I am having this problem . I have the image for the problem.
Hello i am having this problem: i am downloading data from xml using linq
Hello im having a bit of a problem, look at this: SELECT id, activate_key
Hello I'm having a problem here. The code looks like this. private void Form3_Load(object
Hello I am having a problem with Django trying to get it deployed on
Hello I'm having the same problem from this post: Ajax.BeginForm not hiding loading element
hello I am having problem related to https:// . I have used FB.getLoginStatus(function(response) function
Hello I am having a problem with the numbering of figures using Latex, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.