我開發了一個帶有GUI的應用程序,包含按鈕,相對actionListeners和例外。 今天我有這個問題。在一個actionEvent
相對於我的GUI的一個按鈕時,我插入這種代碼,一些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)
您會得到什麼例外? – bmargulies
你得到了什麼例外。你應該把你的catch塊從'System.out.println ...'換成'e.printStackTrace();'。這將打印出完整的例外。請使用堆棧跟蹤更新您的文章。 –
@bmargulies這是,出現在命令行中:System.out.println(「Exception!」);之後,其他代碼不會執行。 –