Numeric Nulls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob H

    Numeric Nulls

    Here is a code snippet I have.

    Float.valueOf(r s.getString(((r smd.getColumnNa me(x+1).toStrin g()!=null
    && !rsmd.getColumn Name(x+1).toStr ing().equals("" ))?rsmd.getColu mnName(x+1).toS tring():"0"))). floatValue()

    and the part:
    rs.getString((( rsmd.getColumnN ame(x+1).toStri ng()!=null &&
    !rsmd.getColumn Name(x+1).toStr ing().equals("" ))?rsmd.getColu mnName(x+1).toS tring():"0"))

    where it is testing the field for null values isn't working. It is
    always getting thru even thow I KNOW the data is null.



    The Problem I have is that no matter how hard I try, if the field is
    NUMERIC in Oracle and the value is NULL, it passes all these tests:

    rsmd.getColumnN ame(x+1).toStri ng()!=null
    !rsmd.getColumn Name(x+1).equal s("")
    !rsmd.getColumn Name(x+1).equal s(null)
    !rsmd.getColumn Name(x+1).toStr ing().equals("" )
    rsmd.getColumnN ame(x+1).toStri ng().length()>0

    I am trying to do a Float.valueOf() and because the field is null i am
    getting this error.

    Servlet.service () for servlet jsp threw exception
    java.lang.NullP ointerException
    at java.lang.Float ingDecimal.read JavaFormatStrin g(FloatingDecim al.java:980)
    at java.lang.Float .valueOf(Float. java:205)

    I know the field is null but it still passes all those tests.
  • Ian Shef

    #2
    Re: Numeric Nulls

    hollywood_88@ho tmail.com (Rob H) wrote in
    news:75c288d7.0 406031103.3ec94 2ef@posting.goo gle.com:
    [color=blue]
    > Here is a code snippet I have.
    >
    > Float.valueOf(r s.getString(((r smd.getColumnNa me(x+1).toStrin g()!=null
    > &&
    > !rsmd.getColumn Name(x+1).toStr ing().equals("" ))?rsmd.getColu mnName(x+1).t
    > oString():"0")) ).floatValue()
    >
    > and the part:
    > rs.getString((( rsmd.getColumnN ame(x+1).toStri ng()!=null &&
    > !rsmd.getColumn Name(x+1).toStr ing().equals("" ))?rsmd.getColu mnName(x+1).t
    > oString():"0"))
    >
    > where it is testing the field for null values isn't working. It is
    > always getting thru even thow I KNOW the data is null.
    >
    >
    >
    > The Problem I have is that no matter how hard I try, if the field is
    > NUMERIC in Oracle and the value is NULL, it passes all these tests:
    >
    > rsmd.getColumnN ame(x+1).toStri ng()!=null
    > !rsmd.getColumn Name(x+1).equal s("")
    > !rsmd.getColumn Name(x+1).equal s(null)
    > !rsmd.getColumn Name(x+1).toStr ing().equals("" )
    > rsmd.getColumnN ame(x+1).toStri ng().length()>0
    >
    > I am trying to do a Float.valueOf() and because the field is null i am
    > getting this error.
    >
    > Servlet.service () for servlet jsp threw exception
    > java.lang.NullP ointerException
    > at
    > java.lang.Float ingDecimal.read JavaFormatStrin g(FloatingDecim al.java:
    > 980) at java.lang.Float .valueOf(Float. java:205)
    >
    > I know the field is null but it still passes all those tests.
    >[/color]

    Perhaps instead of
    rsmd.getColumnN ame(x+1).toStri ng()!=null
    you could check
    rsmd.getColumnN ame(x+1)!=null

    toString() generally does not return null.


    By the way, it is usually clearer to write
    x?y:z
    instead of
    !x?z:y

    --
    Ian Shef
    These are my personal opinions and not those of my employer.

    Comment

    • Derek Chen-Becker

      #3
      Re: Numeric Nulls

      Ian Shef wrote:[color=blue]
      > hollywood_88@ho tmail.com (Rob H) wrote in
      > news:75c288d7.0 406031103.3ec94 2ef@posting.goo gle.com:
      >
      >[color=green]
      >>Here is a code snippet I have.
      >>
      >>Float.valueOf (rs.getString(( (rsmd.getColumn Name(x+1).toStr ing()!=null
      >>&&
      >>!rsmd.getColu mnName(x+1).toS tring().equals( ""))?rsmd.getCo lumnName(x+1).t
      >>oString():"0" ))).floatValue( )
      >>
      >>and the part:
      >>rs.getString( ((rsmd.getColum nName(x+1).toSt ring()!=null &&
      >>!rsmd.getColu mnName(x+1).toS tring().equals( ""))?rsmd.getCo lumnName(x+1).t
      >>oString():"0" ))
      >>
      >>where it is testing the field for null values isn't working. It is
      >>always getting thru even thow I KNOW the data is null.
      >>
      >>
      >>
      >>The Problem I have is that no matter how hard I try, if the field is
      >>NUMERIC in Oracle and the value is NULL, it passes all these tests:
      >>
      >>rsmd.getColum nName(x+1).toSt ring()!=null
      >>!rsmd.getColu mnName(x+1).equ als("")
      >>!rsmd.getColu mnName(x+1).equ als(null)
      >>!rsmd.getColu mnName(x+1).toS tring().equals( "")
      >>rsmd.getColum nName(x+1).toSt ring().length() >0
      >>
      >>I am trying to do a Float.valueOf() and because the field is null i am
      >>getting this error.
      >>
      >>Servlet.servi ce() for servlet jsp threw exception
      >>java.lang.Nul lPointerExcepti on
      >> at
      >> java.lang.Float ingDecimal.read JavaFormatStrin g(FloatingDecim al.java:
      >> 980) at java.lang.Float .valueOf(Float. java:205)
      >>
      >>I know the field is null but it still passes all those tests.
      >>[/color]
      >
      >
      > Perhaps instead of
      > rsmd.getColumnN ame(x+1).toStri ng()!=null
      > you could check
      > rsmd.getColumnN ame(x+1)!=null
      >
      > toString() generally does not return null.
      >[/color]

      Not only that, if rsmd.getColumnN ame(x+1) == null, then
      rsmd.getcolumnN ame(x+1).toStri ng() will throw a NullPointerExce ption
      because you're trying to execute the toString() method on a null pointer...

      Derek

      Comment

      • Chris Smith

        #4
        Re: Numeric Nulls

        Rob H wrote:[color=blue]
        > Here is a code snippet I have.
        >
        > Float.valueOf(r s.getString(((r smd.getColumnNa me(x+1).toStrin g()!=null
        > && !rsmd.getColumn Name(x+1).toStr ing().equals("" ))?rsmd.getColu mnName(x+1).toS tring():"0"))). floatValue()[/color]

        That's pretty ugly. You can extract some of those into local variables
        to make things a little more readable.

        In any case:
        [color=blue]
        > The Problem I have is that no matter how hard I try, if the field is
        > NUMERIC in Oracle and the value is NULL, it passes all these tests:
        >
        > rsmd.getColumnN ame(x+1).toStri ng()!=null
        > !rsmd.getColumn Name(x+1).equal s("")
        > !rsmd.getColumn Name(x+1).equal s(null)
        > !rsmd.getColumn Name(x+1).toStr ing().equals("" )
        > rsmd.getColumnN ame(x+1).toStri ng().length()>0
        >
        > I am trying to do a Float.valueOf() and because the field is null i am
        > getting this error.[/color]

        You're doing a Float.valueOf on a database column name? Do you normally
        name your database columns with floating point numbers?

        Perhaps you really meant to actually call rs.getString(.. .) and then
        compare that to null (or call rs.wasNull just afterward, for a more
        universal way to check for null values).

        --

        The Easiest Way to Train Anyone... Anywhere.

        Chris Smith - Lead Software Developer/Technical Trainer
        MindIQ Corporation

        Comment

        • Liz

          #5
          Re: Numeric Nulls


          "Derek Chen-Becker" <dbecker@cpicor p.com> wrote in message
          news:c9nvrf$t0t $1@cpimail.cpic orp.com...[color=blue]
          > Ian Shef wrote:[color=green]
          > > hollywood_88@ho tmail.com (Rob H) wrote in
          > > news:75c288d7.0 406031103.3ec94 2ef@posting.goo gle.com:
          > >
          > >[color=darkred]
          > >>Here is a code snippet I have.
          > >>
          > >>Float.valueOf (rs.getString(( (rsmd.getColumn Name(x+1).toStr ing()!=null
          > >>&&[/color][/color]
          >[color=green]
          >>!rsmd.getColu mnName(x+1).toS tring().equals( ""))?rsmd.getCo lumnName(x+1).t[color=darkred]
          > >>oString():"0" ))).floatValue( )
          > >>
          > >>and the part:
          > >>rs.getString( ((rsmd.getColum nName(x+1).toSt ring()!=null &&[/color][/color]
          >[color=green]
          >>!rsmd.getColu mnName(x+1).toS tring().equals( ""))?rsmd.getCo lumnName(x+1).t[color=darkred]
          > >>oString():"0" ))
          > >>
          > >>where it is testing the field for null values isn't working. It is
          > >>always getting thru even thow I KNOW the data is null.
          > >>
          > >>
          > >>
          > >>The Problem I have is that no matter how hard I try, if the field is
          > >>NUMERIC in Oracle and the value is NULL, it passes all these tests:
          > >>
          > >>rsmd.getColum nName(x+1).toSt ring()!=null
          > >>!rsmd.getColu mnName(x+1).equ als("")
          > >>!rsmd.getColu mnName(x+1).equ als(null)
          > >>!rsmd.getColu mnName(x+1).toS tring().equals( "")
          > >>rsmd.getColum nName(x+1).toSt ring().length() >0
          > >>
          > >>I am trying to do a Float.valueOf() and because the field is null i am
          > >>getting this error.
          > >>
          > >>Servlet.servi ce() for servlet jsp threw exception
          > >>java.lang.Nul lPointerExcepti on
          > >> at
          > >>[/color][/color][/color]
          java.lang.Float ingDecimal.read JavaFormatStrin g(FloatingDecim al.java:[color=blue][color=green][color=darkred]
          > >> 980) at java.lang.Float .valueOf(Float. java:205)
          > >>
          > >>I know the field is null but it still passes all those tests.
          > >>[/color]
          > >
          > >
          > > Perhaps instead of
          > > rsmd.getColumnN ame(x+1).toStri ng()!=null
          > > you could check
          > > rsmd.getColumnN ame(x+1)!=null
          > >
          > > toString() generally does not return null.
          > >[/color]
          >
          > Not only that, if rsmd.getColumnN ame(x+1) == null, then
          > rsmd.getcolumnN ame(x+1).toStri ng() will throw a NullPointerExce ption
          > because you're trying to execute the toString() method on a null[/color]
          pointer...[color=blue]
          >
          > Derek[/color]

          What Derek says makes it look like the claim of passing the tests
          isn't totally true unless throwing an exception counts as passing.


          Comment

          • Derek Chen-Becker

            #6
            Re: Numeric Nulls

            Liz wrote:[color=blue]
            > "Derek Chen-Becker" <dbecker@cpicor p.com> wrote in message
            > news:c9nvrf$t0t $1@cpimail.cpic orp.com...
            >[color=green]
            >>Ian Shef wrote:
            >>[color=darkred]
            >>>hollywood_88 @hotmail.com (Rob H) wrote in
            >>>news:75c288d 7.0406031103.3e c942ef@posting. google.com:
            >>>
            >>>
            >>>
            >>>>Here is a code snippet I have.
            >>>>
            >>>>Float.value Of(rs.getString (((rsmd.getColu mnName(x+1).toS tring()!=null
            >>>>&&[/color]
            >>[color=darkred]
            >>>!rsmd.getCol umnName(x+1).to String().equals (""))?rsmd.getC olumnName(x+1). t
            >>>
            >>>>oString():" 0"))).floatValu e()
            >>>>
            >>>>and the part:
            >>>>rs.getStrin g(((rsmd.getCol umnName(x+1).to String()!=null &&[/color]
            >>[color=darkred]
            >>>!rsmd.getCol umnName(x+1).to String().equals (""))?rsmd.getC olumnName(x+1). t
            >>>
            >>>>oString():" 0"))
            >>>>
            >>>>where it is testing the field for null values isn't working. It is
            >>>>always getting thru even thow I KNOW the data is null.
            >>>>
            >>>>
            >>>>
            >>>>The Problem I have is that no matter how hard I try, if the field is
            >>>>NUMERIC in Oracle and the value is NULL, it passes all these tests:
            >>>>
            >>>>rsmd.getCol umnName(x+1).to String()!=null
            >>>>!rsmd.getCo lumnName(x+1).e quals("")
            >>>>!rsmd.getCo lumnName(x+1).e quals(null)
            >>>>!rsmd.getCo lumnName(x+1).t oString().equal s("")
            >>>>rsmd.getCol umnName(x+1).to String().length ()>0
            >>>>
            >>>>I am trying to do a Float.valueOf() and because the field is null i am
            >>>>getting this error.
            >>>>
            >>>>Servlet.ser vice() for servlet jsp threw exception
            >>>>java.lang.N ullPointerExcep tion
            >>>> at
            >>>>[/color][/color]
            >
            > java.lang.Float ingDecimal.read JavaFormatStrin g(FloatingDecim al.java:
            >[color=green][color=darkred]
            >>>> 980) at java.lang.Float .valueOf(Float. java:205)
            >>>>
            >>>>I know the field is null but it still passes all those tests.
            >>>>
            >>>
            >>>
            >>>Perhaps instead of
            >>> rsmd.getColumnN ame(x+1).toStri ng()!=null
            >>>you could check
            >>> rsmd.getColumnN ame(x+1)!=null
            >>>
            >>>toString() generally does not return null.
            >>>[/color]
            >>
            >>Not only that, if rsmd.getColumnN ame(x+1) == null, then
            >>rsmd.getcolum nName(x+1).toSt ring() will throw a NullPointerExce ption
            >>because you're trying to execute the toString() method on a null[/color]
            >
            > pointer...
            >[color=green]
            >>Derek[/color]
            >
            >
            > What Derek says makes it look like the claim of passing the tests
            > isn't totally true unless throwing an exception counts as passing.
            >
            >[/color]

            Good catch, I should have been paying more attention to the original
            poster. The first method will throw a NullPointerExce ption and the rest
            of them will never be executed...

            Derek

            Comment

            Working...