Null columns in recordset

Discussion in 'Java' started by Kevin Munro, Nov 7, 2003.

  1. Kevin Munro

    Kevin Munro Guest

    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.
     
    Kevin Munro, Nov 7, 2003
    #1
    1. Advertising

  2. In article <3fab9bdd$0$109$>, 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
     
    Nils O. =?iso-8859-1?Q?Sel=E5sdal?=, Nov 7, 2003
    #2
    1. Advertising

  3. > In article <3fab9bdd$0$109$>, 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
     
    Nils O. =?iso-8859-1?Q?Sel=E5sdal?=, Nov 7, 2003
    #3
  4. Kevin Munro

    Kevin Munro Guest

    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" <> wrote in message
    news:...
    > In article <3fab9bdd$0$109$>, 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
    >
     
    Kevin Munro, Nov 7, 2003
    #4
  5. Kevin Munro

    Chris Smith Guest

    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
     
    Chris Smith, Nov 7, 2003
    #5
  6. Kevin Munro

    Kevin Munro Guest

    Good point, many thanks.

    Kevin.

    "Chris Smith" <> wrote in message
    news: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
     
    Kevin Munro, Nov 11, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    653
    Erik Funkenbusch
    Apr 9, 2006
  2. Replies:
    5
    Views:
    27,232
    Mike Schilling
    Mar 29, 2006
  3. Hung Huynh
    Replies:
    8
    Views:
    341
    Bob Barrows
    Sep 24, 2003
  4. Bryan

    displaying recordset in columns.....

    Bryan, Nov 28, 2003, in forum: ASP General
    Replies:
    4
    Views:
    199
    Bryan
    Dec 1, 2003
  5. shank

    Display recordset in columns

    shank, Mar 14, 2008, in forum: ASP General
    Replies:
    1
    Views:
    364
    Bob Barrows [MVP]
    Mar 14, 2008
Loading...

Share This Page