【发布时间】:2018-09-03 18:02:04
【问题描述】:
我是 java 新手,我只想在用户按下键盘上的 escape 键或单击 showInputDialog 的 X 按钮或按下时显示错误消息取消程序正常关闭,
就像现在如果我关闭或取消 inputDialog 它会给出以下错误
Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:11)
我也尝试抛出异常 JVM,但它没有按我的预期工作,这是我的代码:
String userInput;
BankAccount myAccount = new BankAccount();
while (true){
userInput = JOptionPane.showInputDialog("1. Withdraw\n2. Deposit\n3. View Balance\n4. Exit");
switch (userInput){
case "1":
myAccount.withdraw(Integer.parseInt(JOptionPane.showInputDialog("Please Enter ID: ")),Double.parseDouble(JOptionPane.showInputDialog("Please Enter Amount to Withdraw: ")));
break;
case "2":
myAccount.deposit(Integer.parseInt(JOptionPane.showInputDialog("Please Enter ID: ")),Double.parseDouble(JOptionPane.showInputDialog("Please enter Amount to Deposit: ")));
break;
case "3":
myAccount.viewBalance(Integer.parseInt(JOptionPane.showInputDialog("Please Enter ID: ")));
break;
case "4":
myAccount.exit();
System.exit(0);
default:
JOptionPane.showMessageDialog(null,"Invalid Input\nPlease Try Again");
break;
}
}
如果用户单击 X 或取消提示,我只想显示一条错误消息,我怎样才能捕捉到这个?所以我会在那里实现我的逻辑
【问题讨论】:
-
问题出在第 11 行。我们不知道第 11 行是哪个语句。在您的代码中找到第 11 行并确定该行中的哪个变量为空,然后解决问题。
-
我只想在用户单击 X 或取消提示时显示错误消息,我该如何捕捉?所以我会在那里实现我的逻辑
标签: java swing joptionpane swingx