Java Swing GUI错误空指针

Java Swing GUI错误空指针,java,swing,user-interface,intellij-idea,Java,Swing,User Interface,Intellij Idea,您好,我正在使用我的java代码制作一个windows应用程序,但是我在使用swing GUI时遇到了问题。最终,我尝试制作一个面板,它会出现,并且有一些选项可以用于运行不同的报告。现在,我只是想让我的面板弹出一个带有警报的按钮。这是我的密码。由于这是intellij IDEA,因此GUI实际上没有任何代码,但它是一个.form文件。我想在JButton按钮上添加一个显示hi的事件侦听器。我得到的错误是null pointer EXECTION。我做错了什么?提前谢谢 package com.p

您好,我正在使用我的java代码制作一个windows应用程序,但是我在使用swing GUI时遇到了问题。最终,我尝试制作一个面板,它会出现,并且有一些选项可以用于运行不同的报告。现在,我只是想让我的面板弹出一个带有警报的按钮。这是我的密码。由于这是intellij IDEA,因此GUI实际上没有任何代码,但它是一个.form文件。我想在JButton按钮上添加一个显示hi的事件侦听器。我得到的错误是null pointer EXECTION。我做错了什么?提前谢谢

package com.project.go;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Joe on 12/16/2016.
 */
public class App {
private JButton button;
private JPanel panelMain;
private JTextPane MenuWelcome;
private JComboBox pullDownBox;
private JButton Exit;
private JTextArea DateField;

public static void main(String[] args) {
    JButton button = new JButton("run");
    JFrame frame = new JFrame("App");
    frame.setContentPane(new App().panelMain);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    new App();

}

public App() {
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "hi");
        }
    });
}

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

问题是您正在调用未创建的
panelMain
实例:

frame.setContentPane(new App().panelMain);
您需要首先实例化:

private JPanel panelMain;

也许您想使用我认为是一种好的做法。

使用调试器应该可以立即解决此问题。简而言之:小心你所依赖的。IntelliJ表单插件可能非常邪恶。