I've been trying to use a GUI I made in IntelliJ IDEAS GUI Designer in an applet I'm working on. I've gotten the thing to run when I just have one label inside my root JPanel but for some reason when I add more components I get the following error:
java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1095)
at java.awt.Container.add(Container.java:971)
at inputGui.$$$setupUI$$$(inputGui.java)
at inputGui.<init>(inputGui.java:25)
at HelloWorld.init(HelloWorld.java:11)
at sun.applet.AppletPanel.run(AppletPanel.java:435)
at java.lang.Thread.run(Thread.java:745)
My class for the GUI inputGui.java
is laid out like this:
(I will mark the line where it breaks with: //!BROKEN - Line 25!)
public class inputGui extends JFrame {
private JPanel rootNode;
private JTextField id_field;
private JTextField mi_field;
private JTextField lastName_field;
private JTextField address_field;
//more declorations...
public inputGui() {//initialize GUI
super( "Hello World" );
setContentPane( rootNode );//!BROKEN - Line 25!
pack();
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setVisible( false );
}
public JPanel getRootNode() {
return rootNode;
}
private void createUIComponents() {
// TODO: place custom component creation code here
}
}
And I call it from my Applets 'init()' function:
public class HelloWorld extends JApplet {
//Called when this applet is loaded into the browser.
public void init() {
//Execute a job on the event-dispatching thread; creating this applet's GUI.
inputGui frame = new inputGui();//just here to get a clearer error
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
Container content = getContentPane();
inputGui frame = new inputGui();//init GUI
content.add(frame.getRootNode() );//add rootNode to JFRame
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
}
I just cant figure out why adding components suddenly creates a null pointer!. If its any help my elements are nested like this:
Code from inputGui.java
private void createUIComponents() {
// TODO: place custom component creation code here
}
You selected option "Custom Create" on some component in UI designer. You should create that component by yourself, otherwise it will fail. Uncheck that option and everything should be fine.