CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2009
    Posts
    22

    Question Checking an empty or null string(Request)

    if (!("".equals(req_.getNonEmpData())) ||!(req_.getNonEmpData().equals(null)) )
    {
    itemsNonEmp= req_.getNonEmpData().split(",");
    }
    if ((req_.getEmpData() != null) && (!req_.getEmpData().equals("")))
    {
    itemsEmp=req_.getEmpData().split(",");
    }

    I have checked with every possible scenario for checking null or empty.

    But even if the value of req_.getNonEmpData()=null,its entering the block and trying to split the empty variable..Hence Null pointer exception is being thrown...At the time of debugging,I have hardcoded the value of req_.getNonEmpData() to null,then also its entering in the block.

    Any suggestion ,How to check for empty or null value in req_.getNonEmpData()

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Checking an empty or null string(Request)

    The reason it goes into the IF statement is because your logic is a little flawed. You are saying:

    IF ( NOT - The element is empty OR NOT - The element is null)

    The problem is that it cannot be empty and null at the same time. Think about this: when the element is null what happens when you check:

    !("".equals(element))

    The "".equals(element) will return false, since element is null and not empty. You negate the false to true with the bang operator (!). So when the element is indeed null, your first part of your IF statement evaluates to True, and since you use the || OR operator it will short circuit there and enter the IF statement.

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Checking an empty or null string(Request)

    req_.getNonEmpData().equals(null)

    This test doesn't make sense. What you want to test is if the method: req_.getNonEmpData() returns a null value:
    req_.getNonEmpData() == null
    Norm

  4. #4
    Join Date
    Feb 2008
    Posts
    966

    Re: Checking an empty or null string(Request)

    There's that too

    On a similar note, if the return from getNonEmpData() is indeed null, then calling .equals() on it would result in a NullPointerException, and he wouldn't be getting into the IF statement.

  5. #5
    Join Date
    Nov 2009
    Posts
    22

    Thumbs up Re: Checking an empty or null string(Request)

    Thanks to all,It worked by placing && in between rather than ||

    Thanks all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured