Chris Dev
Chris Dev

Reputation: 1

Java Swing Components Init NullPointerException

I'm trying to have a simple app that lets you pick some files and does some logic with them. I've created the interface via the JFrame Palette builder incorporated in IntelliJ IDEA.

public class App extends JFrame implements ActionListener {
JPanel panelComponent;
private JButton buttonFolder;
private JTextPane textPane;
private JPanel myPanel;
private JButton buttonCreatePackage;
private JTextField textFieldFolder;
private JLabel nameFolderUnderContentLabel;
private JButton buttonAdd;
private JPanel panelList;
private JScrollPane scrollPane;
private JPanel consolePanel;
private JScrollPane tableScrollPane;
private JTable table1; 

}

This is how my App class looks, it being the main point of the application. None of the fields are defined with the "new" initialiser, because IntelliJ does this automatically for me, if I click the see the usages of a component (see the following screenshot): The binding of the variable So, I have my App that extends JFrame, my JButtons that are initialised and binded by the IDE, and I start it like this:

  public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
            myApp = new App();
            myApp.setVisible(true);
            control = true;
            myApp.initViews();
            myApp.document = (StyledDocument) myApp.textPane.getDocument();
            myApp.buttonCreatePackage.setEnabled(false);
            myApp.documentWriter = new DocumentWriter(myApp.document);
            myApp.setContentPane(myApp.myPanel);
        worker.execute();
    });
}

What happens, inside the initViews method I'm setting listeners, but it throws a NullPointerException. Inside the method it just looks like this:

 buttonFolder.addActionListener(e -> {
        //code

}

The exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Clicking on the error, it points me to the line where I set the actionListener on the buttonFolder.

My theory is that somehow there's a collision between the initial thread of the app, the EDT thread where the GUI should be done/edited, and the creation of components between them.

I've checked if the initViews is called on the EDT thread, and the answer is yes. I've tried initialising the views in a Swing worker to explicitly force it on the EDT thread, but it did not work. I've also tried postponing the adding of listeners and such for 200-400ms to give the GUI to init, and i've had no success.

Looking forward to any input, Thank you.

Upvotes: 0

Views: 348

Answers (1)

sleepToken
sleepToken

Reputation: 1847

You say IntelliJ IDEA does the new() for you but... I don't see it in your code.

// Still null
private JButton buttonFolder;

In this current state, buttonFolder is null. You have two options:

// Change your class level button declaration to this:
private JButton buttonFolder = new JButton();

Or, leave it as it is, and from within your main:

// You probably want to go with this method - since you want to identify the button
buttonFolder = new JButton("This is button Folder");

You will have to do this for every JComponent. Finally, I advise against extending JFrame - instead create an instance.

Upvotes: 0

Related Questions