为什么我总是得到这个例外?

时间:2011-06-29 21:14:17

标签: java exception actionlistener joptionpane

我开发了一个带有GUI的应用程序,包括按钮,相对actionListeners和异常。 今天我遇到了这个问题。在相对于我的GUI按钮的actionEvent中,我插入了此代码,其中包含一些JOptionPane.showInputDialog

public void actionPerformed(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!");}             
  }

其中setAvailable, setNotAvailable,setPerishable,setCurrentLoad是外部类的方法,参考auto

当我执行此代码时,它出现在GUI上,然后我点击按钮b1。第一个JOptionPane.showInputDialog似乎是插入存储在int load中的值。

我输入了一个值,但没有出现其他JOptionPane.showInputDialog(但还有其他输入对话框),我在命令行中得到了异常。 我注意到JOptionPane.showInputDialog中插入的值永远不会传递给行auto.setCurrentLoad(load);

为什么会这样?从未见过这个错误。为什么我总是在第一个JOptionPane.showInputDialog之后立即得到异常。也许JVM在同一个语句/方法中不接受许多JOptionPane.showInputDialog?或者(我认为)是我的编程错误?

感谢您的帮助。欢呼声。

编辑:我忘了插入命令行中的异常:

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)

2 个答案:

答案 0 :(得分:1)

根据您的描述,看起来auto变量为null。

答案 1 :(得分:1)

最有可能的是,在按下按钮之前,auto对象未初始化。我假设autoAutoCom类的成员变量。在这种情况下,您应该将auto定义更改为:

protected <TypeOfAutoHere> auto = new <TypeOfAutoHere>();