!s.equals("") returns nullPointerException ???

Discussion in 'Java' started by Tom, Jul 31, 2003.

  1. Tom

    Tom Guest

    Hi,

    I have

    String s="";
    if (!s.equals("")) {

    // do some stuff
    }

    I have that within a try{} block and I get a nullPointerException if I run
    that.... However if check if s!="" then it works OK.... I don't get it. I
    also tried if (!s.length()!=0) ... and the same thing... Any ideas??? I've
    pulled to many hairs today.

    -Tom
     
    Tom, Jul 31, 2003
    #1
    1. Advertising

  2. Tom

    Tom Guest

    ok forget it my string s was actually null so i was calling .equals on a
    null object. ok makes sense. i was under the impression that
    req.getParameter() would return "" instead of null. nope. my wrong
    assumption.

    -Tom

    "Tom" <> wrote in message
    news:...
    > Hi,
    >
    > I have
    >
    > String s="";
    > if (!s.equals("")) {
    >
    > // do some stuff
    > }
    >
    > I have that within a try{} block and I get a nullPointerException if I run
    > that.... However if check if s!="" then it works OK.... I don't get it. I
    > also tried if (!s.length()!=0) ... and the same thing... Any ideas??? I've
    > pulled to many hairs today.
    >
    > -Tom
    >
    >
     
    Tom, Aug 1, 2003
    #2
    1. Advertising

  3. Tom

    Roedy Green Guest

    On Thu, 31 Jul 2003 16:54:14 -0600, "Tom" <> wrote
    or quoted :

    >I have that within a try{} block and I get a nullPointerException if I run
    >that.... However if check if s!="" then it works OK.... I don't get it.


    s.equals(t)

    s Must not be null for this to work. You need an object to find the
    equals method.
    t may be null.

    s == t
    either s or t can be null and it will still work. You are just
    comparing references. You don't need an object since you are not
    invoking any methods.


    See http://mindprod.com/jgloss/gotchas.html#STRINGCOMPARISON


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Aug 1, 2003
    #3
  4. Tom

    xarax Guest

    "Tom" <> wrote in message news:<>...
    > Hi,
    >
    > I have
    >
    > String s="";
    > if (!s.equals("")) {
    >
    > // do some stuff
    > }
    >
    > I have that within a try{} block and I get a nullPointerException if I run
    > that.... However if check if s!="" then it works OK.... I don't get it. I
    > also tried if (!s.length()!=0) ... and the same thing... Any ideas??? I've
    > pulled to many hairs today.
    >
    > -Tom


    You posted pseudo-code. It should work just fine as posted,
    but I suspect that is not what you are using.

    String s = "";

    if(! "".equals(s) )
    {
    // stuff
    }

    would be safer way to compare a known String with
    a variable that may have a null value.
     
    xarax, Aug 1, 2003
    #4
  5. Tom

    Jayaram Guest

    Could you elaborate on what you are doing within and after the 'if {}' clause ?

    > Hi,
    >
    > I have
    >
    > String s="";
    > if (!s.equals("")) {
    >
    > // do some stuff
    > }
    >
    > I have that within a try{} block and I get a nullPointerException if I run
    > that.... However if check if s!="" then it works OK.... I don't get it. I
    > also tried if (!s.length()!=0) ... and the same thing... Any ideas??? I've
    > pulled to many hairs today.
    >
    > -Tom
     
    Jayaram, Aug 1, 2003
    #5
  6. Tom

    Jon A. Cruz Guest

    Re: called method on null object

    Tom wrote:
    > I'm interested in Xarax's suggestion though, of checking if(!"".equals(s))
    > ... will this return an exception of s is null? I guess you are no longer
    > calling a method on an object which is null... I never thought of that way
    > of checking for null or "".


    It will return false if s is null. It's a very nice solution, since it
    leverages the fact that in Java, all String literals are actually object
    instances.

    Of course, if you want to ensure you have both non-null and non-empty,
    then the code changes to

    if ( s != null && !"".equals(s) )
     
    Jon A. Cruz, Aug 1, 2003
    #6
  7. Tom

    Tom Guest

    Re: called method on null object

    > Of course, if you want to ensure you have both non-null and non-empty,
    > then the code changes to
    >
    > if ( s != null && !"".equals(s) )
    >


    ....but if !"".equals(s) return false if s is null then why would you need to
    check if s is null?

    -Tom
     
    Tom, Aug 1, 2003
    #7
  8. Tom

    Drew Volpe Guest

    Last time we met, Roedy Green <> had said:
    > On Thu, 31 Jul 2003 16:54:14 -0600, "Tom" <> wrote
    > or quoted :
    >
    > >I have that within a try{} block and I get a nullPointerException if I run
    > >that.... However if check if s!="" then it works OK.... I don't get it.

    >
    > s.equals(t)
    >
    > s Must not be null for this to work. You need an object to find the
    > equals method.
    > t may be null.


    this is why it's often better to write:

    "".equals(myString);






    dv

    --------------------------------------------------------------------------
    The geographical center of Boston is in Roxbury. Due north of the
    center we find the South End. This is not to be confused with South
    Boston which lies directly east from the South End. North of the South
    End is East Boston and southwest of East Boston is the North End.

    Drew Volpe, mylastname at hcs o harvard o edu
     
    Drew Volpe, Aug 1, 2003
    #8
  9. Tom

    Tim Tyler Guest

    Re: called method on null object

    Tom <> wrote or quoted:

    :> Of course, if you want to ensure you have both non-null and non-empty,
    :> then the code changes to
    :>
    :> if ( s != null && !"".equals(s) )

    : ...but if !"".equals(s) return false if s is null then why would you need to
    : check if s is null?

    It doesn't.

    "".equals(null) returns false;

    !"".equals(null) returns true.
    --
    __________
    |im |yler http://timtyler.org/
     
    Tim Tyler, Aug 2, 2003
    #9
  10. Tom

    Chris Smith Guest

    Re: called method on null object

    Tom wrote:
    > > Of course, if you want to ensure you have both non-null and non-empty,
    > > then the code changes to
    > >
    > > if ( s != null && !"".equals(s) )
    > >

    >
    > ...but if !"".equals(s) return false if s is null then why would you need to
    > check if s is null?


    Because you probably don't want a null reference to fall into the same
    case as a reference to a non-empty String.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Aug 3, 2003
    #10
  11. Tom

    Chris Smith Guest

    Drew Volpe wrote:
    > Last time we met, Chris Smith <> had said:
    > > Drew Volpe wrote:
    > > >
    > > > this is why it's often better to write:
    > > >
    > > > "".equals(myString);

    > >
    > > Just IMHO, that's a good way to avoid finding bugs in your code. If
    > > you're expecting the String to be non-null, and it turns out to be null,
    > > that's a problem, and one that you most likely want to know about. The
    > > NullPointerException tells you about it. I'm at a loss for why you
    > > prefer a coding style that avoids the NullPointerException, and instead
    > > exhibits behavior that's almost certainly wrong (that is, treating a
    > > null reference just like a reference to a non-empty String).

    >
    > Because it's not almost certaintly wrong. Often null String variables
    > are expected and perfectly valid.


    <sigh> That's not the issue. Of course reference variables can often
    be null. The issue is how often you find yourself with two very
    specific cases in the logic of your code, those being:

    1. String reference is null, or points to a non-empty String.
    2. String reference points to an empty String.

    [Of course, the literal could be other than the empty String, but I'm
    going to assume for the purpose of this discussion that it's the empty
    String, because that seems the most likely in a class of unlikely
    scenarios.]

    And furthermore, the question is whether this situation is enough of the
    norm that it's worth recommending a language usage that encodes this as
    a general preference, in the way that you did. The problem that this
    solves is, in fact, so unique that I'd probably avoid a concise-looking
    expression that doesn't deal with the odd intricacies of the desired
    logic, and instead write the statement as:

    if ((myString == null) || !myString.equals(""))
    {
    // CASE 1
    }
    else
    {
    // CASE 2
    }

    If you very frequently run into this specific situation, where a null
    reference should be treated the same as a non-empty String, but
    differently from an empty String, I'm really curious about where it pops
    up so frequently. In the mean time, I'm concerned that recommendations
    for the "literal".equals(variable) style will be picked up by those who
    aren't thinking about the possibility of a null reference, and they will
    write code that produces more logic errors and fewer clear and helpful
    runtime errors. Those who are already thinking about null references
    and considering them as possible inputs are probably competent enough to
    express their logic on their own.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Aug 4, 2003
    #11
  12. Tom

    soft-eng Guest

    Chris Smith <> wrote in message news:<>...
    > soft-eng wrote:
    > > I figure that
    > >
    > > "".equals(s)
    > >
    > > hurts readability just enough imho (at least for left-to-right
    > > readers, who have to mentally swap the logic) that I take the
    > > time to write out
    > >
    > > s == null || s.equals("")

    >
    > Except that they do different things:


    Yes, of course, you have to know what you want to happen
    with null, and explicit writing it out makes sure
    that you do know it, as does any reader of your code.

    Usually, if I am checking for "", I would many times
    want the same thing for null (for instance when
    checking for the presence of a parameter.) That's
    when an automatic

    "".equals(s)

    would actually 'work' with null, but result
    in a wrong choice in case a null does happen
    to come along.

    > > This expanded version usually takes longer than just typing the extra
    > > characters, because the required form may be
    > >
    > > s != null && ! s.equals("")
    > >
    > > or something else.

    >
    > Actually, an expanded form would be one of:
    >
    > (s == null) || !s.equals("")
    > (s != null) && s.equals("")


    ?? Why would it be restricted to that? What if I want to make sure s is
    neither null nor an empty string?
     
    soft-eng, Aug 5, 2003
    #12
  13. Tom

    Chris Smith Guest

    I'm not sure what direction you're going...

    soft-eng wrote:
    > Usually, if I am checking for "", I would many times
    > want the same thing for null (for instance when
    > checking for the presence of a parameter.) That's
    > when an automatic
    >
    > "".equals(s)
    >
    > would actually 'work' with null, but result
    > in a wrong choice in case a null does happen
    > to come along.


    Right. For some definitions of "work", including only definitions where
    your program is broken but doesn't tell you, it would work fine.

    > > Actually, an expanded form would be one of:
    > >
    > > (s == null) || !s.equals("")
    > > (s != null) && s.equals("")

    >
    > ?? Why would it be restricted to that? What if I want to make sure s is
    > neither null nor an empty string?


    Because I was talking about expanded forms of "".equals(s) [or its
    negation], and your stated requirements don't match that expression. So
    I wouldn't consider any correct code to be an expansion of "".equals(s).
    That follows by necessity, since "".equals(s) is itself broken.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Aug 5, 2003
    #13
    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. Edward A Thompson
    Replies:
    4
    Views:
    558
    Tony Morris
    Feb 11, 2004
  2. vizlab
    Replies:
    3
    Views:
    4,270
    Michael Bar-Sinai
    Oct 17, 2007
  3. ++imanshu
    Replies:
    7
    Views:
    499
    ++imanshu
    Aug 23, 2008
  4. Trans
    Replies:
    2
    Views:
    251
    Trans
    Nov 6, 2005
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    672
    David A. Black
    Jul 2, 2008
Loading...

Share This Page