Java - JOptionPane.showInputDialog 个错误

Java - JOptionPane.showInputDialog errors

大家好,我的 JOptionPane.showInputDialog 输入字段出现问题。 The issue i am experiencing that is when the cancel button is selected or the OK button is selected with empty or with categorical i am greeted with an error.有什么建议么。 我第一次尝试解决此问题时,我将 c int 更改为整数,因此我可以使用 if 语句和 null,例如

Integer cInterger = new Integer(c);
  if (cInterger.equals(null)){
      return;}

一起
cInterger == null
cInterger !=null

但是没有用

int a = 0;  //
int b = 0; //
int c = 0; //
    if (selected) {
        if (command.equals("amount")) { 
            Scanner readFile = null;
                try {
                    readFile = new Scanner(new FileReader("BANK.txt"));
                    } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                    String account = "";
                    account = readFile.nextLine(); 
                    String amount = JOptionPane.showInputDialog(frame,"Enter an amount"); //assign user input to string amount
                    a= Integer.parseInt(account); //conversion of scanned string to int for easy subtraction                
                    b= Integer.parseInt(amount); 
                    c = a-b; 

                        try {  
                                if( c == 0){
                                    JOptionPane.showMessageDialog(null,"Granted.","Granted",JOptionPane.WARNING_MESSAGE);}
                                else if (c > 0){
                                    JOptionPane.showMessageDialog(null,"Granted.","Granted",JOptionPane.INFORMATION_MESSAGE); }
                                else{
                                    JOptionPane.showMessageDialog(null,"Denied.","Denied",JOptionPane.ERROR_MESSAGE);}  

产生的错误

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null
        at java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at OptionsMyItemListener.itemStateChanged(Options.java:58)
        at javax.swing.AbstractButton.fireItemStateChanged(Unknown Source)
        at javax.swing.AbstractButton$Handler.itemStateChanged(Unknown Source)
        at javax.swing.DefaultButtonModel.fireItemStateChanged(Unknown Source)
        at javax.swing.JToggleButton$ToggleButtonModel.setSelected(Unknown Source)
        at javax.swing.ButtonGroup.setSelected(Unknown Source)
        at javax.swing.JToggleButton$ToggleButtonModel.setSelected(Unknown Source)
        at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        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.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access0(Unknown Source)
        at java.awt.EventQueue.run(Unknown Source)
        at java.awt.EventQueue.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.run(Unknown Source)
        at java.awt.EventQueue.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain.doIntersectionPrivilege(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)

如果你看一下 JavaDocs,你会发现

Returns:
user's input, or null meaning the user canceled the input

所以尝试解析 null 没有任何意义,相反你应该使用类似...

if (amount != null) {
    a = Integer.parseInt(amount);
    //...

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" 似乎相当明显。

if (amount != null) {
   if (amount.trim().length() > 0) {
       //...
   } else {
       JOptionPane.showMessageDialog(frame, "The amount you entered is invalid");
   }

您还应该检查 account