I\'m querying a database and some of the results I\'m getting are null. I\'m setting these values to a variable with a datatype of double. Let\'s call the variable \"results
To say that something "is null" means that it is a reference to the null value. Primitives (int, double, float, etc) are by definition not reference types, so they cannot have null values. You will need to find out what your database wrapper will do in this case.
I would recommend using a Double not a double as your type then you check against null.
I believe Double.NaN might be able to cover this. That is the only 'null' value double contains.
A double primitive in Java can never be null. It will be initialized to 0.0 if no value has been given for it (except when declaring a local double variable and not assigning a value, but this will produce a compile-time error).
More info on default primitive values here.
Firstly, a Java double
cannot be null, and cannot be compared with a Java null
. (The double
type is a primitive (non-reference) type and primitive types cannot be null.)
Next, if you call ResultSet.getDouble(...), that returns a double
not a Double
, the documented behaviour is that a NULL (from the database) will be returned as zero. (See javadoc linked above.) That is no help if zero is a legitimate value for that column.
So your options are:
use ResultSet.wasNull() to test for a (database) NULL ... immediately after the getDouble(...)
call, or
use ResultSet.getObject(...), and type cast the result to Double
.
The getObject
method will deliver the value as a Double
(assuming that the column type is double
), and is documented to return null
for a NULL. (For more information, this page documents the default mappings of SQL types to Java types, and therefore what actual type you should expect getObject
to deliver.)
How are you getting the value of "results"? Are you getting it via ResultSet.getDouble()? In that case, you can check ResultSet.wasNull().