接收此错误的Java基本GUI程序"AWT-EventQueue-0"java.lang.NullPointerException"

dim*_*mas 0 java swing awt nullpointerexception event-dispatch-thread

我正在尝试构建一个简单的gui程序.一切都运行良好,因为我在添加一些GUI组件(如SWING和AWT)之前测试了这些类.但是当我尝试输入并按下提交按钮时,它会给我这个错误.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.simpleAccountEntry.SimpleAccountListener.actionPerformed(SimpleAccountListener.java:15)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6389)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3268)
    at java.awt.Component.processEvent(Component.java:6154)
    at java.awt.Container.processEvent(Container.java:2045)
    at java.awt.Component.dispatchEventImpl(Component.java:4750)
    at java.awt.Container.dispatchEventImpl(Container.java:2103)
    at java.awt.Component.dispatchEvent(Component.java:4576)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4633)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4297)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4227)
    at java.awt.Container.dispatchEventImpl(Container.java:2089)
    at java.awt.Window.dispatchEventImpl(Window.java:2518)
    at java.awt.Component.dispatchEvent(Component.java:4576)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
    at java.awt.EventQueue.access$400(EventQueue.java:96)
    at java.awt.EventQueue$2.run(EventQueue.java:631)
    at java.awt.EventQueue$2.run(EventQueue.java:629)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
    at java.awt.EventQueue$3.run(EventQueue.java:645)
    at java.awt.EventQueue$3.run(EventQueue.java:643)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
Run Code Online (Sandbox Code Playgroud)

我从昨天开始尝试调试它,但我找不到我犯了错误的地方.我试着检查这个错误代码:

 com.simpleAccountEntry.SimpleAccountListener.actionPerformed(SimpleAccountListener.java:15)
Run Code Online (Sandbox Code Playgroud)

它指向这个类:

public class SimpleAccountListener implements ActionListener{

    private SimpleAccountActionsPanel listen;

    public SimpleAccountListener(SimpleAccountActionsPanel functionPanel){
        listen = functionPanel;
    }

    public void actionPerformed(ActionEvent e){
        listen.recordPatient(); //SimpleAccountListener.java: 15
    }
}
Run Code Online (Sandbox Code Playgroud)

listen.recordPatient()可以在这个类中找到,还有我编写的其他方法

编辑

 public class SimpleAccountActionsPanel extends JPanel{

        private SimpleAccountPanel account = new SimpleAccountPanel();
**//Initialize this line
        static private SimpleAccountActionsPanel perform = new SimpleAccountActionsPanel();**

        private DetailsEntry<Details> setPatient = new DetailsEntry<Details>();

        static private JButton submit;
        static private JButton delete;

        public SimpleAccountActionsPanel(){
            this.setLayout(new GridLayout(1,2));


            submit = new JButton("Submit Entries");
            delete = new JButton("Delete Entries");

            submit.setBackground(Color.DARK_GRAY);
            submit.setForeground(Color.ORANGE);

            delete.setBackground(Color.DARK_GRAY);
            delete.setForeground(Color.ORANGE);

            this.add(submit);
            this.add(delete);

            //SimpleAccountPanel varFields = new SimpleAccountPanel();
            SimpleAccountListener performFn = new SimpleAccountListener(perform);
            submit.addActionListener(performFn);
        }

        public void recordPatient(){
            String name = account.getEnterName().getText();
            String dob = account.getEnterDOB().getText();
            String doc = account.getEnterDr().getText();
            String allergy = account.getAllergies().getText();
            String room = account.getEnterRoomNo().getText();
            int convRoom = Integer.parseInt(room);

            setPatient.addEntry(new Details(name, dob, doc, allergy, convRoom));
        }
    }
Run Code Online (Sandbox Code Playgroud)

有人在乎帮助我吗?如果您需要我实施的更多课程,我可以发布它只是让我知道提前谢谢.

编辑

感谢Max和MadProgrammer,我按照建议的程序进行了初始化,现在可以完美地运行.

Mad*_*mer 6

您正在传递SimpleAccountListenerperform未在构造函数中初始化的变量的引用SimpleAccountActionsPanel,因此是NPE

SimpleAccountListener performFn = new SimpleAccountListener(perform); // <-- perform is not initialised...
Run Code Online (Sandbox Code Playgroud)

我想弄清楚你为什么不通过this

SimpleAccountListener performFn = new SimpleAccountListener(this);
Run Code Online (Sandbox Code Playgroud)


ten*_*ica 5

你有这个声明:

private SimpleAccountActionsPanel perform;
Run Code Online (Sandbox Code Playgroud)

但是perform从未初始化,因此它是空的NullPointerException.你把它传递给了SimpleAccountListener.