-1

I'm fetching two double values from JDBC, if they are null I want to use these values as null in my textfield and if they are 0 I wanted to populate use them as 0. I've tried resultSet.wasNull but even if I'm getting 0 it is setting them as null value.

This is what I've written.

resultsetPricing = stmtPricingData.executeQuery();  
while(resultsetPricing.next()){  
    if(pricetype.equals("Price List A")){  
        priceExclVat = resultsetPricing.getDouble("PRICE_EXC_VAT");  
        priceInclVat = resultsetPricing.getDouble("PRICE_INC_VAT");  
        if(priceExclVat == 0){  
            System.out.println("Inside if EXCL");  
            if(resultsetPricing.wasNull()){  
                System.out.println("Inside RESULT SET NULL EXCL");  
                //do something
            }  
        }else if(priceExclVat != 0 || priceExclVat == 0){  
            System.out.println("Inside ELSE EXCL");  
            priceExclVatStr =  String.valueOf(priceExclVat).replace(".",",");  
            //do something
        }  
        if(priceInclVat==0){  
            System.out.println("Inside if INCL");  
            if(resultsetPricing.wasNull()){  
                System.out.println("Inside RESULT SET NULL INCL");  
                 //do something
            }  
        }else if(priceInclVat!=0 || priceInclVat==0){  
            System.out.println("Inside ELSE INCL");  
            priceInclVatStr = String.valueOf(priceInclVat).replace(".",",");  
             //do something  
        }         
    }  
}  

Please point out where I'm wrong. TIA

tj_lucas
  • 11
  • 6
  • How do you know what the actual value is in the database? – Jim Garrison Feb 27 '18 at 07:25
  • I've checked the db. The columns returning 0 or null values are being checked as null, but the value greater than 0 is fetched correctly – tj_lucas Feb 27 '18 at 07:31
  • Short answer is use `getObject` not `getDouble`. That will give you a `null` if and only if the value in the resultset is `null`. If the value is not `null` then you will get a `Double` ... – Stephen C Feb 27 '18 at 07:36

1 Answers1

0

The test of the return value of wasNull() must be the first thing you do.

A double cannot have a null value, so zero is returned from the getDouble() method when you call it.

The code should look like this pseudocode:

priceExclVat = resultsetPricing.getDouble("PRICE_EXC_VAT");  
boolean pevNull = resultSetPricing.wasNull();
priceInclVat = resultsetPricing.getDouble("PRICE_INC_VAT");  
boolean pivNull = resultSetPricing.wasNull();

if (pevNull && pivNull)
{
    // code to deal with both null return
}
else if (pevNull && !pivNull)
{
    // code to handle non-null PEV and null PIV
}
else if (!pevNull && pivNull)
{
    // etc...
}
....
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • I've put a check also for this. if you see my code I'm checking the resultset is 0 or not if it is 0 then I'm checking it is null or not by using **resultSet.wasNull** . You can refer the above code. – tj_lucas Feb 27 '18 at 07:34
  • You're not handling all the possible combinations of `null` and non-`null` in your code. The code needs to consider ALL combinations. – Jim Garrison Feb 27 '18 at 07:35
  • I've tried it but only possible solution that I found was resultSet.wasNull to check if result is have null value or not – tj_lucas Feb 27 '18 at 07:38