-
September 9th, 2010, 05:18 AM
#1
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()
-
September 9th, 2010, 07:48 AM
#2
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.
-
September 9th, 2010, 08:18 AM
#3
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
-
September 9th, 2010, 08:36 AM
#4
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.
-
September 14th, 2010, 02:18 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|