8

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.

schlebe
  • 3,387
  • 5
  • 37
  • 50
Quinn
  • 111
  • 1
  • 1
  • 5
  • The simple answer is, `panel` is never initialised. The longer is more complicated (thanks to the intellij's form editor - I never did actually work out how that thing actually worked). Unless you're willing to spend some more time working through intellij's form editor tutorials, I'd personally ditch it and learn to code your UI's by hand – MadProgrammer Feb 11 '18 at 21:04
  • I understand that panel is never initialized. I even tried initializing it manually, to no effect. – Quinn Feb 11 '18 at 21:08
  • "When" did you try to initialise it? Before or after `frame.setContentPane(new View().panel);`? In your example, it would need to be initialised in the `View`'s constructor – MadProgrammer Feb 11 '18 at 21:10
  • 1
    Did you move your .java to another package? – ricardogobbo Nov 27 '18 at 18:17

3 Answers3

0

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'.

konmal88
  • 111
  • 4
0

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

chehonte
  • 56
  • 5
0

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);

SmugDoodleBug
  • 329
  • 4
  • 21