How can i know the String object is created or is null

Discussion in 'Java' started by mahesh, Dec 8, 2006.

  1. mahesh

    mahesh Guest

    HI alll

    in my code i want to check that the sql query return null value
    and if the query return null value the i hav to use

    stmt.setXXX(1,Types.some oracle type)
    else
    i hav to set other value for that column using
    stmt.setXXX(1,****)
    to find null values i'm using
    String st=rst.getInt(1).toString();
    but if it returns null then it set the st=null
    and it trhows null pointer exception
    is there any way to achieve this
    mahesh, Dec 8, 2006
    #1
    1. Advertising

  2. mahesh

    Bart Cremers Guest

    On Dec 8, 2:01 pm, "mahesh" <> wrote:
    > HI alll
    >
    > in my code i want to check that the sql query return null value
    > and if the query return null value the i hav to use
    >
    > stmt.setXXX(1,Types.some oracle type)
    > else
    > i hav to set other value for that column using
    > stmt.setXXX(1,****)
    > to find null values i'm using
    > String st=rst.getInt(1).toString();
    > but if it returns null then it set the st=null
    > and it trhows null pointer exception
    > is there any way to achieve this


    If you want to receive a String from a resultset it's better to use
    rst.getString(index). So:

    String st = rst.getString(1);

    if (st == null) {
    doSomething();
    } else {
    doSomethingElse();
    }


    Regards,

    Bart
    Bart Cremers, Dec 8, 2006
    #2
    1. Advertising

  3. mahesh wrote:
    ....
    > in my code i want to check that the sql query return null value
    > and if the query return null value the i hav to use


    if ( theString==null ) {
    // ...

    Have you considered doing the basic Java tutorial?
    <http://java.sun.com/docs/books/tutorial/index.html>

    Andrew T.
    Andrew Thompson, Dec 8, 2006
    #3
  4. mahesh wrote:
    > HI alll
    >
    > in my code i want to check that the sql query return null value
    > and if the query return null value the i hav to use
    >
    > stmt.setXXX(1,Types.some oracle type)
    > else
    > i hav to set other value for that column using
    > stmt.setXXX(1,****)
    > to find null values i'm using
    > String st=rst.getInt(1).toString();
    > but if it returns null then it set the st=null
    > and it trhows null pointer exception
    > is there any way to achieve this
    >


    If that line itself throws the exception, you are not even getting to
    the point of calling toString.

    Is it possible that rst is null? If so, you need to test for that first.

    As others have indicated, test for null by == comparison.

    Patricia
    Patricia Shanahan, Dec 8, 2006
    #4
  5. mahesh

    mahesh Guest

    Bart Cremers wrote:
    > On Dec 8, 2:01 pm, "mahesh" <> wrote:
    > > HI alll
    > >
    > > in my code i want to check that the sql query return null value
    > > and if the query return null value the i hav to use
    > >
    > > stmt.setXXX(1,Types.some oracle type)
    > > else
    > > i hav to set other value for that column using
    > > stmt.setXXX(1,****)
    > > to find null values i'm using
    > > String st=rst.getInt(1).toString();
    > > but if it returns null then it set the st=null
    > > and it trhows null pointer exception
    > > is there any way to achieve this

    >
    > If you want to receive a String from a resultset it's better to use
    > rst.getString(index). So:
    >
    > String st = rst.getString(1);
    >
    > if (st == null) {
    > doSomething();
    > } else {
    > doSomethingElse();
    > }
    >
    >
    > Regards,
    >
    > Bart





    thank u
    mahesh, Dec 11, 2006
    #5
  6. mahesh

    mahesh Guest

    Bart Cremers wrote:
    > On Dec 8, 2:01 pm, "mahesh" <> wrote:
    > > HI alll
    > >
    > > in my code i want to check that the sql query return null value
    > > and if the query return null value the i hav to use
    > >
    > > stmt.setXXX(1,Types.some oracle type)
    > > else
    > > i hav to set other value for that column using
    > > stmt.setXXX(1,****)
    > > to find null values i'm using
    > > String st=rst.getInt(1).toString();
    > > but if it returns null then it set the st=null
    > > and it trhows null pointer exception
    > > is there any way to achieve this

    >
    > If you want to receive a String from a resultset it's better to use
    > rst.getString(index). So:
    >
    > String st = rst.getString(1);
    >
    > if (st == null) {
    > doSomething();
    > } else {
    > doSomethingElse();
    > }
    >
    >
    > Regards,
    >
    > Bart





    thank u
    mahesh, Dec 11, 2006
    #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:
    16
    Views:
    7,328
    Mike Schilling
    Oct 12, 2005
  2. Replies:
    5
    Views:
    26,628
    Mike Schilling
    Mar 29, 2006
  3. Replies:
    2
    Views:
    339
    Andreas Leitgeb
    Jan 21, 2008
  4. Replies:
    2
    Views:
    118
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    229
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page