0

I am still pretty new to Java. I got a sample at class, to create a simple user interface and a button that should change text upon click.

The GUI opens fine, but once I click on the button, I get a NullPointerException error.

This is my code:

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

public class GUISimple1 implements ActionListener {
JButton button;

public static void main (String[] args) {
    GUISimple1 gui = new GUISimple1();
    gui.empezar();
}

public void empezar() { 
    JFrame frame = new JFrame();
    JButton button = new JButton ("Click"); 

    button.addActionListener(this);

    frame.getContentPane() .add(BorderLayout.NORTH, button);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,300);
    frame.setVisible(true);
}   

    public void actionPerformed(ActionEvent event) {
        button.setText("Me han dado un click!");
}
}

And this is the error, I receive when I run the application:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GUISimple1.actionPerformed(GUISimple1.java:27)
at     javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3315)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Any help would be appreciated.

Cheers and thanks in advance.

BenjB
  • 155
  • 9
  • Code is exactly as in the excercise, just changed the English labels to Spanish. – BenjB Feb 04 '17 at 22:56
  • You have no way of guaranteeing that `button` is defined by the time `actionPerformed` is called. – Makoto Feb 04 '17 at 22:58
  • No code is not likely to be "exactly as in the exercise". You're shadowing the button variable so that the class field remains null. Don't re-declare the variable in the constructor. – Hovercraft Full Of Eels Feb 04 '17 at 23:04
  • i.e., change `JButton button = new JButton ("Click"); ` to `button = new JButton ("Click"); ` Note the difference! – Hovercraft Full Of Eels Feb 04 '17 at 23:04
  • Thx. Hovercraft, it works.. and I am aware of your comment and pretty much always, I play around with code and have to make changes to it. It was just, that this was a sample code, to introduce GUI Generation with Java, without any work on it... It explicitly asked to put it this way. – BenjB Feb 04 '17 at 23:16
  • If you are going to post that as an answer, I will go ahead and accept it. Thanks again. – BenjB Feb 04 '17 at 23:16
  • No, this same problem has been answered thousands of times before, one more won't help the site any, which is why the question has been appropriately closed as a duplicate. Hopefully now you know what "variable shadowing" is, and have one more coding debugging tool clipped to your tool belt. – Hovercraft Full Of Eels Feb 05 '17 at 02:55
  • I do know that.. and not to trust in thebooks blindly.. and I did indeed improve my toolbelt. Thanks again. – BenjB Feb 05 '17 at 18:14

0 Answers0