简体   繁体   English

为什么在启动GUI时出现NullPointerException?

[英]Why do I get a NullPointerException when I start my GUI?

I have coded a gui for my CRUD Program, and when I want to run it I get: 我已经为我的CRUD程序编写了gui编码,当我想运行它时,我得到:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at gui.guimain$1.run(guimain.java:477)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Thats the code where the NPE comes from: 那就是NPE来自的代码:

public static void main(String[] args){
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run(){
                maingui.showStart();// thats the line with the Exception
        }
    });
}

and thats the method showStart(): 就是方法showStart():

public void showStart(){
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.getContentPane().removeAll();
    tabstructure.removeAll();
    Produkt filter=new Produkt();

    JPanel P=new JPanel();
    P=Produktgui(0, filter);
    JPanel R=new JPanel();
    R=Billgui(0);
    JPanel nR=new JPanel();
    nR=Billgui(0);

    tabstructure.addTab("e", P);
    tabstructure.addTab("Bills", R);
    tabstructure.addTab("Pay bill", nR);
    mainFrame.getContentPane().add(tabstructure);
    mainFrame.validate();
    mainFrame.repaint();
    mainFrame.pack();
    mainFrame.setVisible(true);
}

Why do I get a NullPointerException ? 为什么会收到NullPointerException

You will have to create an instance of your MainGui . 您将必须创建MainGui的实例。

public static void main(String[] args) {
    final MainGui maingui = new MainGui();
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run(){
                maingui.showStart();// thats the line with the Exception
        }
    });
}

Because something that you're calling a method on is null . 因为您要在其上调用方法的内容为null

Look carefully at the error message. 仔细查看错误消息。 It says that the exception happens on line 477 of guimain.java . 它说异常发生在guimain.java 477行上。

At that point, maingui is null . 此时, mainguinull

You must instantiate objects before you can invoke methods on them. 您必须实例化对象,然后才能在它们上调用方法。 In this case, maingui hasn't been instantiated. 在这种情况下, maingui尚未实例化。

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            MainGUI maingui = new MainGUI();
            maingui.showStart();
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM