Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Null columns in recordset

Reply
Thread Tools

Null columns in recordset

 
 
Kevin Munro
Guest
Posts: n/a
 
      11-07-2003
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.


 
Reply With Quote
 
 
 
 
Nils O. =?iso-8859-1?Q?Sel=E5sdal?=
Guest
Posts: n/a
 
      11-07-2003
In article <3fab9bdd$0$109$(E-Mail Removed)>, Kevin Munro wrote:
> 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.


--
Vennlig hilsen/Best Regards
Nils Olav Selåsdal <NOS at Utel.no>
System Engineer
UtelSystems a/s

 
Reply With Quote
 
 
 
 
Nils O. =?iso-8859-1?Q?Sel=E5sdal?=
Guest
Posts: n/a
 
      11-07-2003
> In article <3fab9bdd$0$109$(E-Mail Removed)>, Kevin Munro wrote:
>> 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.


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);
....
}



--
Vennlig hilsen/Best Regards
Nils Olav Selåsdal <NOS at Utel.no>
System Engineer
UtelSystems a/s

 
Reply With Quote
 
Kevin Munro
Guest
Posts: n/a
 
      11-07-2003
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.



"Nils O. Selåsdal" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) g...
> In article <3fab9bdd$0$109$(E-Mail Removed)>, Kevin Munro

wrote:
> > 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.
>
>
> --
> Vennlig hilsen/Best Regards
> Nils Olav Selåsdal <NOS at Utel.no>
> System Engineer
> UtelSystems a/s
>



 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      11-07-2003
Kevin Munro wrote:
> 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
 
Reply With Quote
 
Kevin Munro
Guest
Posts: n/a
 
      11-11-2003
Good point, many thanks.

Kevin.

"Chris Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)4.net...
> Kevin Munro wrote:
> > 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Display recordset in columns shank ASP General 1 03-14-2008 08:00 PM
How to I tell if a field within a recordset is NULL ? rouble@gmail.com ASP .Net 1 04-08-2006 11:00 PM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
displaying recordset in columns..... Bryan ASP General 4 12-01-2003 03:23 PM
RecordSet.Move or RecordSet.AbsolutePosition?? Hung Huynh ASP General 8 09-24-2003 11:07 AM



Advertisments