【问题标题】:Why do I always get this exception?为什么我总是得到这个异常?
【发布时间】:2011-06-29 21:14:17
【问题描述】:

我开发了一个带有 GUI、按钮、相关 actionListener 和异常的应用程序。 今天我遇到了这个问题。在相对于我的 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)

【问题讨论】:

  • 你得到的异常是什么。您应该将 catch 块从 System.out.println... 更改为 e.printStackTrace();。这将打印出完整的异常。请使用堆栈跟踪更新您的帖子。
  • @bmargulies 这是,出现在命令行中: System.out.println("Exception!");之后,其他代码没有执行..
  • 你需要调试。所以你需要在调试器中运行并查看异常,看看它是什么异常。当您发现时,将其发布在此处,您可能会得到帮助。
  • @bmargulies 抱歉,这是个例外:“java.lang.NullPointerException”
  • 好吧,它还会告诉你源代码的哪一行得到了 NPE,这可能会告诉你发生了什么。

标签: java exception actionlistener joptionpane


【解决方案1】:

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

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

【讨论】:

  • 我修好了。我忘了初始化对象,没有将“private”更改为“protected”
  • 谢谢你,有很多行代码我忘记了这个重要的事情。
【解决方案2】:

根据您的描述,auto 变量似乎为空。

【讨论】:

  • 但我首先在 AutoCom 类中声明为:“private Auto auto”,其中“Auto”是一个外部类。
  • 好的。只是你说的。我解决了。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-01
  • 2017-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多