I have coded a gui for my CRUD Program, and when I want to run it I get:
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:
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():
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
?
You will have to create an instance of your 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
.
Look carefully at the error message. It says that the exception happens on line 477 of guimain.java
.
At that point, maingui
is null
.
You must instantiate objects before you can invoke methods on them. In this case, maingui
hasn't been instantiated.
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainGUI maingui = new MainGUI();
maingui.showStart();
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.