This seems too trivial.
After populating a resultset with a query, what is the best way to test
if a column is null to avoid the dreaded null pointer exception when
attempting a tostring() call.
thanks
> This seems too trivial.
> After populating a resultset with a query, what is the best way to test
> if a column is null to avoid the dreaded null pointer exception when
> attempting a tostring() call.
> thanks
ResultSet.wasNull() (very important if you use getInt() or other get
methods that return primitive data types)
As an alternative you can always test your return value (if it's an
Object) agains null ;), e.g:
String value = rs.getString(1);
if (value != null) ....
Thomas

Signature
It's not a RootKit - it's a Sony
gself - 16 Aug 2006 14:50 GMT
> > This seems too trivial.
> > After populating a resultset with a query, what is the best way to test
[quoted text clipped - 15 lines]
> --
> It's not a RootKit - it's a Sony
I was hoping for something like an isnull test.
String value = rs.getString(1); requires an extra creation of a String
var
wasNull requires the getString first to set it's status anyway
Oh well, I'm justing longing for the good old days of 'C' where if you
could think of it you could do it. :-)
Thanks
Thomas Kellerer - 16 Aug 2006 15:02 GMT
> I was hoping for something like an isnull test.
> String value = rs.getString(1); requires an extra creation of a String
> var
> wasNull requires the getString first to set it's status anyway
> Oh well, I'm justing longing for the good old days of 'C' where if you
> could think of it you could do it. :-)
How should the driver decide that without fetching the value from the
server? There is no way around the getXXXX()
Btw: I doubt that rs.getString(1) will create a *new* String (in the
sense that additional memory is allocated to hold the character data)
If the driver simply does a "return value" where value is the String
that it created, no additional memory is required (except for your local
reference).
Even a "return new String(value)" does not copy the character data. The
new String merely references the character array of the source (this is
possible as Strings are imutable)
Thomas

Signature
It's not a RootKit - it's a Sony
> This seems too trivial.
> After populating a resultset with a query, what is the best way to test
> if a column is null to avoid the dreaded null pointer exception when
> attempting a tostring() call.
If all you are going to do with your ResultSet is to print it, I would
do
System.out.println(rs.getObject(x)+" somestring"+...
which would produce
null somestring
I've tried this with integer, varchar and double columns, but I don't
know if it will work for all types (I guess that depends on how the
getObject() method is implemented in the driver)

Signature
dt