Results 1 to 10 of 10
  1. #1
    swati.jyoti is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Question Entering in the loop,even if the value is null or empty

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

    I dont know ,Y the control is entering in the block when i am hardcoding the
    empdata to null
    as req.setEmpData(null);
    And also at the debug time it showing the value as-->
    "req_.getEmpData()"= null

    Dont know what is the problem,Y the control is entering in the loop??

    Pls do help me with the Problem

    Thanks in advance. :)

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Don't use == (or !=) for Strings. Change the second half of that to use the equals method.

  3. #3
    swati.jyoti is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Default

    Its giving the error when i am debugging --->java.lang.NullPointerException at equals(Object)

    if (!(req_.getEmpData().equals(null) )||!(req_.getEmpData().equals("")))
    {
    }

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I said to do that for the second half of the statement, not the first half. The null check must be done with == or != as you cannot dereference null as that error shows.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Also, you want to use "and" (&&) not "or" (||) as all strings will be either "not null" or "not empty". An "empty" string is "not null" and a null string is "not empty".

  6. #6
    swati.jyoti is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Default

    None is working..I have tried each and every possibe cases suggested by u..

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Ye Gods
    Java Code:
    if ((req_.getEmpData() != null) && (!req_.getEmpData().equals("")))

  8. #8
    swati.jyoti is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Default

    Exception in thread "main" java.lang.NullPointerException..
    Its pointing to that line only
    since its entering in the loop
    and then trying to split the null string.

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    :sigh: Show the entire code in that area. and mark the npe line. PS an if statement is a statement that controls a block, it is not a loop. It is couter-productive to think of it as one.

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Also, if that line, as I typed it, is throwing an NPE, then it is req_ that is null, not the result of req_.getEmpData().

Similar Threads

  1. Arrays and use of null or empty inputs
    By Desmond in forum New To Java
    Replies: 6
    Last Post: 07-27-2010, 04:54 PM
  2. entering textfile into database
    By UJJAL DHAR in forum New To Java
    Replies: 11
    Last Post: 06-15-2010, 01:55 AM
  3. Entering a space as argument
    By Arne Bjarne in forum Eclipse
    Replies: 1
    Last Post: 03-02-2009, 12:43 PM
  4. Entering string in a loop with method
    By Konex in forum New To Java
    Replies: 3
    Last Post: 03-01-2009, 07:33 AM
  5. Entering a while loop with a not equal to string
    By bri1547 in forum New To Java
    Replies: 9
    Last Post: 07-09-2008, 07:10 AM

Posting Permissions

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