Dus*_* E. 2 java user-interface applet intellij-idea
我一直在尝试在我正在使用的applet中使用我在IntelliJ IDEAS GUI Designer中创建的GUI.当我在我的根JPanel中只有一个标签但是由于某种原因当我添加更多组件时,我得到了运行的东西我得到以下错误:
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)
Run Code Online (Sandbox Code Playgroud)
我的GUI类inputGui.java是这样的:
(我将标记它打破的行://!BROKEN - 第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
}
}
Run Code Online (Sandbox Code Playgroud)
我从我的Applets的init()函数中调用它:
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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我只是想不通为什么添加组件突然创建一个空指针!如果有任何帮助我的元素嵌套如下:

cos*_*llo 21
inputGui.java中的代码
private void createUIComponents() {
// TODO: place custom component creation code here
}
Run Code Online (Sandbox Code Playgroud)
您在UI设计器中的某个组件上选择了"自定义创建"选项.您应该自己创建该组件,否则它将失败.取消选中该选项,一切都应该没问题.