3

I created a JPanel with a subpanel inside it using the GUI-designer of IntelliJ. Now I want the subpanel to be a new JPanel and readd to my top panel. But this causes a NullPointerExceptiion. Why and how can I solve it? Note: None of my components are marked as "Custom Create"

Exception in thread "main" java.lang.NullPointerException
    at com.intellij.uiDesigner.core.GridLayoutManager.addLayoutComponent(GridLayoutManager.java:134)
    at java.awt.Container.addImpl(Container.java:1127)
    at java.awt.Container.add(Container.java:417)
    at View.<init>(View.java:16)
    at Main.main(Main.java:4)

View Class

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
public class View{
    private JPanel panel;
    private JPanel subPanel;

    public JPanel getPanel() {
        return panel;
    }

    View()
    {
        panel.remove(subPanel);
        subPanel = newPanel();
        panel.add(subPanel);
    }
    JPanel newPanel(){
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        JLabel label = new JLabel("test");
        panel.add(label);
        return panel;
    }

    private void createUIComponents(){
    }
}

View.form

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="View">
  <grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="2" column-count="1" 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>
      <grid id="860ea" binding="subPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
        <margin top="0" left="0" bottom="0" right="0"/>
        <constraints>
          <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
        </constraints>
        <properties/>
        <border type="none"/>
        <children/>
      </grid>
      <vspacer id="7838f">
        <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>
    </children>
  </grid>
</form>

Main

import javax.swing.*;
public class Main {
    public static void main(String[] args) {
        View view = new View();
        JFrame frame = new JFrame("Window");
        frame.setContentPane(view.getPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
honiahaka10
  • 772
  • 4
  • 9
  • 29
  • You should take a look at what exactly is happening at **GridLayoutManager.java:134** – OH GOD SPIDERS Jun 01 '17 at 16:48
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) The problem seems to occur on the line `panel.remove(subPanel);` (which should be obvious from the stack trace). Where is `subpanel` or `panel` instantiated? 3) For these types of situations generally: Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Jun 01 '17 at 16:55
  • 1) I will take a look. 2) The Error occurs at line `panel.add(subpanel)` 2.2) I think if I do not check `Custom Create` I do not need to instantiate the components myself? At least I never needed before... Why do I need it here? Instantiating in the constructor seems to solve the problem, but checking `Custom Create` and instantiating it in `createUIComponents()` does not. But problem with that is , that I would have to create my whole UI again programmatically. 3) That does not seem to fit for me. This is just a small example. Later I want to add two overlapping `JLabels` with `ImageIcon` – honiahaka10 Jun 01 '17 at 17:49
  • Okay it seems to be a problem with the `Layout Manager`. Changing it to another than `GridLayoutManager (IntelliJ)` solves the problem. – honiahaka10 Jun 01 '17 at 18:20

2 Answers2

4

GridLayoutManager (IntelliJ) does not seem to allow adding new components without instantiating the JPanel manually first. Changing to GridBagLayout solved the problem.

honiahaka10
  • 772
  • 4
  • 9
  • 29
1

You need to add GridConstraints

https://intellij-support.jetbrains.com/hc/en-us/community/posts/206192269-Hello-noob-question-about-intellij-layout-manager

panel.add(sth, new com.intellij.uiDesigner.core.GridConstraints());

there is maven dependency for this:

<!-- https://mvnrepository.com/artifact/com.intellij/forms_rt -->
    <dependency>
        <groupId>com.intellij</groupId>
        <artifactId>forms_rt</artifactId>
        <version>7.0.3</version>
    </dependency>
wojtess
  • 72
  • 3