Search code examples
javaswingif-statementjoptionpane

Java - JOptionPane.showInputDialog errors


Hello all I am experiencing issues with my JOptionPane.showInputDialog input field. 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. Any suggestions. My first attempt at fixing this i changed the c int into an interger therefore i could use an if statement along with null such as

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

Along with

cInterger == null
cInterger !=null

But to no avail

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);}  

Resulting errors

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 Options$1MyItemListener.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.access$400(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.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)

Solution

  • If you take a look at the JavaDocs, you will find that

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

    So attempting to parse null makes no sense, instead you should be using something like...

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

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" seems reasonably obvious.

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

    You should also check the account value as well