We help IT Professionals succeed at work.

Java: Detecting a null value in a double column

JamesFrog
JamesFrog asked
on
Medium Priority
660 Views
Last Modified: 2012-06-27
Hi,

I have a database table (where number is a double(10,2) column)

ID |  number
1   |  456.98
2   |  874.99
3   |  765.98
4   |  NULL
5   |  879.98

to get a number of use

recordset.getDouble("number");

but how do I detect the null value?

i.e.

if number is null {
 // the number is null
} else {
 the number isn't null
}


Thanks
J
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
Try the following first
Object o = rs.getObject(doubleCol);
if (o == null) {
    // etc
}

Open in new window

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
recordset.getDouble("number");
if (recordset.wasNull()) {
    // null
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you'd of course assign the value returned by getDouble to a var if you needed it.

Author

Commented:
... my ACTUAL code is as here - so I'm trying to get it NOT to set highestHigh when the PriceHigh in the table is a NULL value
if ((resultset.getDouble("PriceHigh")>highestHigh)&&(resultset.getDouble("PriceHigh").!equals("NULL"))){
   								highestHigh = resultset.getDouble("PriceHigh");
   							}

Open in new window

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you need to pull them as var before the if

double highprice = resultset.getDouble("PriceHigh");
if (recordset.wasNull()) {
    // null
} else {

Author

Commented:
Thanks, that works perfectly: final code that works is

if (resultset.getDouble("PriceHigh")>highestHigh){
                                            if (resultset.wasNull()) {
                                               // null
                                                  }else{
                                                   highestHigh = resultset.getDouble("PriceHigh");}
                                             }
                                             
                                             if (resultset.getDouble("PriceLow")
CERTIFIED EXPERT
Top Expert 2016

Commented:
Why don't you just select and order by price btw?

Author

Commented:
Because I need the null values in the recordset to signal other aspects of what is going on here (like week-ends and bank holidays) and detection of last trading day of any particular week.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.