Hello, noob question about intellij layout manager

I'm using this code to add some buttons in a JPanel I've created using the visual GUI designer on intelliJ:
        for (int i=0; i<10; i++) {
            JButton myButton = new JButton();
            myButton.setText(text);
           containerPanel.add(myButton);
        }



When I do this, I get this runtime exception:

Exception in thread "main" java.lang.NullPointerException
 at com.intellij.uiDesigner.core.GridLayoutManager.addLayoutComponent(GridLayoutManager.java:134)
 at java.awt.Container.addImpl(Container.java:1120)
 at java.awt.Container.add(Container.java:410)
 at jis.SettingsExplorer.addSomething(SettingsExplorer.java:44)
 at jis.SettingsExplorer.ShowDialog(SettingsExplorer.java:28)
 at Rapide.showPreferences(Rapide.java:68)
 at Rapide.main(Rapide.java:51)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:606)
 at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)


So I'm wondering how can I dynamically add components to a JPanel I've created in the visual GUI designer? What I'm doing wrong? It's my second day in Java so it may be a very silly question, but I can guess where my bug is.
Also, if you think this community is not the right place for this kind of questions, please, feel free to give me any suggestions.

Thanks in advance!
1 comment
Comment actions Permalink

Hello,

> When I do this, I get this runtime exception:

> Exception in thread "main" java.lang.NullPointerException


Yes, you did add component to panel without layout constraints.


Try:         containerPanel.add(myButton, new com.intellij.uiDesigner.core.GridConstraints(...));
0

Please sign in to leave a comment.