Results 1 to 10 of 10
- 09-08-2010, 12:04 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 42
- Rep Power
- 0
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. :)
- 09-08-2010, 12:30 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Don't use == (or !=) for Strings. Change the second half of that to use the equals method.
- 09-08-2010, 01:17 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 42
- Rep Power
- 0
Its giving the error when i am debugging --->java.lang.NullPointerException at equals(Object)
if (!(req_.getEmpData().equals(null) )||!(req_.getEmpData().equals("")))
{
}
- 09-08-2010, 01:29 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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.
- 09-08-2010, 01:32 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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".
- 09-08-2010, 02:28 PM #6
Member
- Join Date
- Mar 2009
- Posts
- 42
- Rep Power
- 0
None is working..I have tried each and every possibe cases suggested by u..
- 09-08-2010, 02:31 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Ye Gods
Java Code:if ((req_.getEmpData() != null) && (!req_.getEmpData().equals("")))
- 09-08-2010, 02:40 PM #8
Member
- Join Date
- Mar 2009
- Posts
- 42
- Rep Power
- 0
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.
- 09-08-2010, 05:07 PM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
: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.
- 09-08-2010, 05:10 PM #10
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Similar Threads
-
Arrays and use of null or empty inputs
By Desmond in forum New To JavaReplies: 6Last Post: 07-27-2010, 04:54 PM -
entering textfile into database
By UJJAL DHAR in forum New To JavaReplies: 11Last Post: 06-15-2010, 01:55 AM -
Entering a space as argument
By Arne Bjarne in forum EclipseReplies: 1Last Post: 03-02-2009, 12:43 PM -
Entering string in a loop with method
By Konex in forum New To JavaReplies: 3Last Post: 03-01-2009, 07:33 AM -
Entering a while loop with a not equal to string
By bri1547 in forum New To JavaReplies: 9Last Post: 07-09-2008, 07:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks