Null columns in recordset

K

Kevin Munro

Hello,

How do I handle nulls in my recordset?

try {
myrs.next();
hasBananas=myrs.getString(1);
}

etc... throws a NullPointerException if the database column returns null.

Should I just manage it by exception or is there a better way?

Thanks, Kevin.
 
N

Nils O. =?iso-8859-1?Q?Sel=E5sdal?=

Hello,

How do I handle nulls in my recordset?

try {
myrs.next();
hasBananas=myrs.getString(1);
}

etc... throws a NullPointerException if the database column returns null.

Should I just manage it by exception or is there a better way?
I don't see how the above code could trow a nullpointer exception.
What _should_ happen if the column no 1 here is null is that
hasBananas should be set to null.
Thus, you'd get a nullpointerexception later if you did e.g.
hasBananas.length();
or something similar.
So, jst check for null before you use hasBananas.
if(hasBananas == null)
handle it.
 
N

Nils O. =?iso-8859-1?Q?Sel=E5sdal?=

I don't see how the above code could trow a nullpointer exception.
What _should_ happen if the column no 1 here is null is that
hasBananas should be set to null.
Thus, you'd get a nullpointerexception later if you did e.g.
hasBananas.length();
or something similar.
So, jst check for null before you use hasBananas.
if(hasBananas == null)
handle it.

OR.. if you meant that it does not return any rows ?
This is usually handled with doing
while(rs.next()){
hasBananas=myrs.getString(1);
....


}
Or if only one column is expected
if(rs.next()){
hasBananas=myrs.getString(1);
....
}
 
K

Kevin Munro

Thanks, looking at my code I was probably getting the exception later on
when I tested for it, but I was doing this in the same try block.

I'm now doing...

if (myrs.getString(6)==null) {hasBananas="N"} else
{hasBananas=myrs.getString(6);}

though there is probably a shorthand way of doing this.

I didn't realise I could actually put a null into a String and then test for
it being null (but that's my vb background for you!)

Kevin.



 
C

Chris Smith

Kevin said:
Thanks, looking at my code I was probably getting the exception later on
when I tested for it, but I was doing this in the same try block.

I'm now doing...

if (myrs.getString(6)==null) {hasBananas="N"} else
{hasBananas=myrs.getString(6);}

though there is probably a shorthand way of doing this.

I don't know the extent of this problem, but I've found that some
database drivers have problems if you don't traverse the ResultSet in
order, asking for each value exactly once. Hence, I'd be tempted to
write that code as:

hasBananas = myrs.getString(6);
if (hasBananas == null) hasBananas = "N";

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
K

Kevin Munro

Good point, many thanks.

Kevin.

Chris Smith said:
I don't know the extent of this problem, but I've found that some
database drivers have problems if you don't traverse the ResultSet in
order, asking for each value exactly once. Hence, I'd be tempted to
write that code as:

hasBananas = myrs.getString(6);
if (hasBananas == null) hasBananas = "N";

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top