Why do I always get this exception?
I developed an app with a GUI, with buttons, relative actionListeners, and exceptions.
Today I had this problem. In an actionEvent
relative to a button of my GUI, I inserted this code, with some JOptionPane.showInputDialog
:
public void actionP开发者_如何学JAVAerformed(ActionEvent ae){
if(ae.getSource()==b1){
try{//FIRST `JOptionPane.showInputDialog`
int load = Integer.parseInt(JOptionPane.showInputDialog(null,"Insert current load value: "));
auto.setCurrentLoad(load);
//other `JOptionPane.showInputDialog`
int choiceDep = Integer.parseInt(JOptionPane.showInputDialog(null, "Does the truck transport perishable goods? 1: YES 2: NO"));
if(choiceDep==1) {
//here we have to insert expiration date
int day = Integer.parseInt(JOptionPane.showInputDialog(null,"Insert value"));
int month = Integer.parseInt(JOptionPane.showInputDialog(null,"Insert value"));
int year = Integer.parseInt(JOptionPane.showInputDialog(null,"Insert value"));
auto.setPerishable(day,month,year);
}
else if(choiceDep==2)
auto.setNotPerishable();
String choiceAv = JOptionPane.showInputDialog(null, "Available Truck? Yes or no?");
if(choiceAv.equals("Yes")) auto.setAvailable();
else auto.setNotAvailable();
}
//the exception
catch (Exception e) { System.out.println("Exception!");}
}
Where setAvailable, setNotAvailable,setPerishable,setCurrentLoad
are methods of the external class, with reference auto
.
When I execute this code, it appears the GUI, then I click on button b1
. It appears the first JOptionPane.showInputDialog
, to insert a value stored in a int load
.
I entered a value, but no other JOptionPane.showInputDialog
appeared (but there are other input dialog) and I got the exception in the command-line.
I noticed that the value inserted in the JOptionPane.showInputDialog
is never passed to the line auto.setCurrentLoad(load);
.
Why does it happen? Never seen this error before. Why do I always get the exception immediately after the first JOptionPane.showInputDialog
Maybe the JVM doesn't accept many of this JOptionPane.showInputDialog
in the same statement/method? Or maybe(as I think) is a programming error of mine?
Thanks for your help. Cheers.
EDIT: I forgot to insert the exception I got in the command-line:
java.lang.NullPointerException
at AutoCom.actionPerformed(AutoCom.java:50)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown So
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Most likely, the auto
object is not initialized before you pressed the button. I'm assuming auto
is a member variable of the AutoCom
class. In that case, you should probably change the auto
definition to:
protected <TypeOfAutoHere> auto = new <TypeOfAutoHere>();
Based on your description it looks like the auto variable is null.
精彩评论