簡體   English   中英

使用IntelliJ IDEA GUI Designer在$$$ setupUI $$$上的NullPointer異常

[英]NullPointer Exception at $$$setupUI$$$ using IntelliJ IDEA GUI Designer

我一直在嘗試在我正在使用的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)

我的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
    }
}

我從我的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");
        }
    }

}

我只是想不通為什么添加組件會突然創建一個空指針! 如果有任何幫助我的元素嵌套如下:

在此輸入圖像描述

inputGui.java中的代碼

private void createUIComponents() {
   // TODO: place custom component creation code here
}

您在UI設計器中的某個組件上選擇了“自定義創建”選項。 您應該自己創建該組件,否則它將失敗。 取消選中該選項,一切都應該沒問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM