Intellij, "contentPane cannot be set to null" using swing designer

14.6k Views Asked by At

I'm trying out Intellij Idea, and am trying to do some Swing development. I am running into an issue I have never experienced on Eclipse, and I am wondering if I have something set up wrong.

Here is my GUI class that is run by my driver:

package view;

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

public class View {

    private JPanel panel;

    public void run() {
        JFrame frame = new JFrame("Vending Machine");
        frame.setContentPane(new View().panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    {
    // GUI initializer generated by IntelliJ IDEA GUI Designer
    // >>> IMPORTANT!! <<<
    // DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

/**
 * Method generated by IntelliJ IDEA GUI Designer
 * >>> IMPORTANT!! <<<
 * DO NOT edit this method OR call it in your code!
 *
 * @noinspection ALL
 */
    
private void $$$setupUI$$$() {
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridBagLayout());
    }
}

As far as I can tell, my run() method is as straightforward as it gets. However, upon compiling, this is the error I receive:

Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
at javax.swing.JRootPane.setContentPane(JRootPane.java:621)
at javax.swing.JFrame.setContentPane(JFrame.java:698)
at view.View.run(View.java:13)
at model.VendingMachine.<init>(VendingMachine.java:14)
at controller.Driver.main(Driver.java:14)

For whatever reason, the Intellij auto created code that does not properly initialize the JPanel, which is why it's null.

I've tried instantiating it myself inside run (panel = new JPanel();) but that has not helped.

Is this something obvious? I've never run into this issue when getting started with Swing in Eclipse.

4

There are 4 best solutions below

0
konmal88 On

You are setting as JFrame' s content pane the panel 'panel' but the JPanel you are creating is called 'panel1'. To fix this problem change JPanel' s name to 'panel' instead of 'panel1'.

0
kapitoshka On

Try enabling "UI Designer" plugin in Intellij IDEA, it helped in my case. File -> Settings -> Plugins -> UI Designer -> Restart IDE

0
SmugDoodleBug On

You need to make sure to to set the field name of the panel. You are possibly misunderstanding the following line:

frame.setContentPane(new View().panel);

In this code, new View().panel is really trying to initialize the object with the field name. So if the panel doesn't have that name...obviously you are trying to instantiate something that doesn't exist.

I named my JPanel MainPanel under the field name property in the jform editor and wrote:

frame.setContentPane(new View().MainPanel);

0
Adir Dayan On

In order to use IntelliJ UI Designer you must compile the code using IntelliJ, in my problem I used gradle for build.

Steps:

  1. Go to Settings: Editor --> GUI Designer --> Generate GUI into (replace from Binary to Java)
  2. Add gradle dependency - implementation group: 'com.intellij', name: 'forms_rt', version: '7.0.3'