0

I am trying to stop a stacktrace which doesn't seem to be listening to my null check.

          if(amountEntered != null){

                   amntEntered = Double.parseDouble(amountEntered);       
            }

            else if(amountEntered != ""){

                amntEntered = Double.parseDouble(amountEntered);
            }

            else if((amountEntered == null || amountEntered == "")){
             System.out.print("");    
            }

With this code, it should stop the stacktrace which executes when I try and cancel out of the JOptionPane (amountEntered is the variable in which the JOptionPane is assigned to) -- amntEntered is the double equivelent.

  • 1
    [How to compare Strings in java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Titus Mar 16 '15 at 18:13

2 Answers2

1

You are comparing string so instead of amountEntered != "" you should be using

!amountEntered.equals("");

this applies whenever you want to compare strings in Java...

but especially for equality to null try string == null

adrCoder
  • 3,145
  • 4
  • 31
  • 56
  • I've tried the .equals method and it's still doing the same thing. After putting it in a try catch block it seems to have fixed parted of the issue on the 'OK' button, but the 'Cancel' button is still producing a stack-trace. –  Mar 16 '15 at 18:21
  • try the `==` for `null` and the `equals` for the other strings... and tell me if it works – adrCoder Mar 16 '15 at 18:34
0

Your logic is a bit off, first off, in Java Strings are compared using equals(..):

if(amountEntered != null){
      amntEntered = Double.parseDouble(amountEntered);  
      // Because you are comparing amountEntered to "" and you check if it's null
      //I assume it is of type String which means that you can't cast it to a double.
}else if(!amountEntered.equals("")){ 
        // if it gets past the first check
        // it means that amountEntered is null and this will produce a NullPointerException
    amntEntered = Double.parseDouble(amountEntered);
}else if((amountEntered == null || amountEntered.equals(""))){
        // Here if amountEntered is null, the second check will
        // still be executed and will produce a NullPointerException
        // When you use || both the check before || and after || are executed
    System.out.print("");    
}

Here is how you can perform the check and deal with any Exceptions:

if(amountEntered != null && !amountEntered.isEmpty()){
   try{
      someDoubleVariable = Double.parseDouble(amountEntered);
   }catch(NumberFormatException e){
      someDoubleVariable = 0;
      e.printStackTrace()
   }
}else if(amountEntered==null || (amountEntered!=null && amountEntered.isEmpty())){
     someDoubleVariable = 0;
}

In this example, because I'm using && the condition checking will stop as soon as one of them is false which means that in the last else if if amountEntered is null amountEntered.isEmpty() will not be executed

Titus
  • 22,031
  • 1
  • 23
  • 33