0

Problem: Not able to update swing component from another class.

Objective: When user clicks the update button, the text in "nameLabel" will change to "John Doe".

I attempted the solutions from stackoverflow regarding this issue but had no success.

Here is a very simple example, not sure what I'm doing wrong.

GUI.java

package TestGUI.TestGUI;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GUI {

    JLabel nameLabel;
    JButton updateButton;
    JFrame frame;
    JPanel panel;

    public void display() {

        frame = new JFrame();
        panel = new JPanel(new BorderLayout()); 
        frame.setTitle("BlackJack Game");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);


        nameLabel = new JLabel("No Name");
        updateButton = new JButton("Update");

        panel.add(nameLabel,  BorderLayout.NORTH);
        panel.add(updateButton,  BorderLayout.SOUTH);

        updateButton.addActionListener(new EventListener());
    }

    public void setNameLabel(String name) {
        nameLabel.setText(name);
    }
}

EventListener.java

package TestGUI.TestGUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EventListener implements ActionListener {

    //private Card card;
    private GUI gui;

    public EventListener(GUI gui) {
        this.gui = gui;
    }

    public EventListener() {
        // TODO Auto-generated constructor stub
    }

    public void actionPerformed(ActionEvent actionEvent) {

        String actionCommand = actionEvent.getActionCommand();

        if(actionCommand.equals("Update")) {
            gui.setNameLabel("John Doe");
        }

    }
}

App.java

package TestGUI.TestGUI;

public class App 
{
    public static void main( String[] args )
    {
        GUI gui = new GUI();

        gui.display();
    }
}

The following error is produced, when the "update" button is clicked.

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Zahidul Islam
  • 99
  • 1
  • 9
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Jan 30 '20 at 08:36

1 Answers1

2

I have seen your code. There is one thing to modify to solve this issue.

In this code, you've an updateButton which is performing the operation as per specified in the EventListener class defined by you.

In the class GUI, inside display() method, you are adding action listener to the updateButton as

 updateButton.addActionListener(new EventListener());

But, in the EventListener class defined by you, this constructor is not doing anything. You have define another constructor that takes an object of GUI class and initializes the class variable gui with that. In actionPerformed(), you are operating on the same gui object.

So, when you are adding an object of EventListener() to updateButton without providing object of the GUI class, EventListener class variable 'gui' remains uninitialized in the default constructor and that is why null pointer exception is thrown when you click on the update button.

So, in order to solve this, you need to pass a reference of the GUI class that you wish to be updated inside display() method. So, if you simply do this modification, it will work as desired.

updateButton.addActionListener(new EventListener(this));
Rajen Ranpara
  • 83
  • 2
  • 8