0

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.

Chris Dev
  • 1
  • 1
  • Before buttonFolder.addActionListener, add `System.out.println(buttonFolder);`. Then run the program again. If the program’s output shows `null`, buttonFolder wasn’t initialized properly. If that is the case, I would first suspect that App.form contains a typo, like `binding="buttonfolder"` or something. – VGR Jan 15 '20 at 13:01

1 Answers1

0

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.

sleepToken
  • 1,866
  • 1
  • 14
  • 23
  • It appears from the screen shot that the project has an `App.form` file, which appears to be IntelliJ’s way of saving the results of using a GUI builder. I assume there is code, not shown in the question, which reads this and applies it to the fields. – VGR Jan 15 '20 at 12:59
  • @VGR That makes more sense. A result of me not being able to view images on my current VPN. – sleepToken Jan 15 '20 at 13:03
  • sleepToken Unfortunately, being on my first post I'm unable to embed pictures into the post, so they get automatically referenced via the link after the "(see the following screenshot)" part. As @VGR is saying, IntelliJ builds this automatically, and binds it with a name and an id. I've double checked the name and it's good. I've also tried to SOUT the button and it is indeed null. What I do not know, is why this happens. – Chris Dev Jan 15 '20 at 14:02