0

Here is my project: https://github.com/Kolyall/GUIExample

MainClass

import javax.swing.*;

public class MainClass {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(MainFrame::new);
    }

}

MainFrame

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

public class MainFrame extends JFrame {

    public MainFrame() {
        super("MainFrame");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(850, 650));
        setSize(new Dimension(850, 650));


        JPanel rootPanel = new JPanel();
        rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.X_AXIS));
        getContentPane().add(rootPanel);
        setLocationRelativeTo(null);
        setVisible(true);

        ButtonsFrame buttonsFrame = new ButtonsFrame()
        rootPanel.add(buttonsFrame.buttonsPanel);//java.lang.NullPointerException in this line
    }

}

ButtonsFrame

import javax.swing.*;

public class ButtonsFrame {
    public JButton button1Button;
    public JButton button2Button;
    public JPanel buttonsPanel;
}

ButtonsFrame.form

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.github.kolyall.test.ButtonsFrame">
  <grid id="27dc6" binding="buttonsPanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
    <margin top="0" left="0" bottom="0" right="0"/>
    <constraints>
      <xy x="20" y="20" width="500" height="400"/>
    </constraints>
    <properties/>
    <border type="none"/>
    <children>
      <component id="70295" class="javax.swing.JButton" binding="button1Button" default-binding="true">
        <constraints>
          <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
        </constraints>
        <properties>
          <text value="Button1"/>
        </properties>
      </component>
      <vspacer id="3d1dd">
        <constraints>
          <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
        </constraints>
      </vspacer>
      <component id="76c52" class="javax.swing.JButton" binding="button2Button" default-binding="true">
        <constraints>
          <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
        </constraints>
        <properties>
          <text value="Button2"/>
        </properties>
      </component>
    </children>
  </grid>
</form>

But after run of MainClass.main() The error occurs:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1095)
    at java.awt.Container.add(Container.java:419)
    at com.github.kolyall.test.MainFrame.<init>(MainFrame.java:29)

Seems like IntelliJ IDEA doesn't generate binary class files, but the option is on, I also tried witn "Java source code" enter image description here

UPDATE: It's known issue of Idea Why swing GUI form builder doesn't generate binary class files/java source code?

NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
  • Do you use Gradle for building? See https://intellij-support.jetbrains.com/hc/en-us/community/posts/206227509-How-to-make-javac2-work-in-Gradle-SOLVED- and - if you want to continue to build by Gradle and see https://stackoverflow.com/a/58018426/2000323 if you want to use IDE builder. – Andrey Jan 26 '20 at 08:13
  • @Andrey its known issue https://youtrack.jetbrains.com/issue/IDEA-207997 – NickUnuchek Jan 26 '20 at 08:29

1 Answers1

0

It's not about binary code genearation. Your code looks ugly and wrong.

ButtonsFrame buttonsFrame = new ButtonsFrame()
rootPanel.add(buttonsFrame.buttonsPanel);

You get a NPE here because you've not initialized the buttonsPanel in your ButtonsFrame class.

For example:

public class ButtonsFrame {
    public JButton button1Button = new JButton("OK");
    public JButton button2Button = new JButton("Cancel");
    public JPanel buttonsPanel = new JPanel();
}

But public member variables are not welcome in OOP. Better approach is to provide method that creates a ready-to-use buttons panel.

public class ButtonsFrame {
    private JButton button1Button = new JButton("OK");
    private JButton button2Button = new JButton("Cancel");
    private JPanel buttonsPanel = new JPanel();

    public JPanel createButtonsPanel() {
        buttonsPanel.removeAll();
        buttonsPanel.add(button1Button);
        buttonsPanel.add(button2Button);
        return buttonsPanel;
    }
}

And use it later so:

rootPanel.add(buttonsFrame.createButtonsPanel());
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • `private JButton button1Button = new JButton("OK");` is highlighted as `Field 'button1Button' is overwritten by generated code` – NickUnuchek Jan 24 '20 at 14:00
  • swing GUI form builder should generate `binary class files`/`java source code`, but it didn't. See https://habr.com/ru/post/305974/ – NickUnuchek Jan 24 '20 at 14:01
  • It's a (IMHO) bad Idea to start with any UI builder. UI builder for Swing can only be used for prototyping, and only when you can Swing basics. – Sergiy Medvynskyy Jan 24 '20 at 14:11