| Author |
How CheckNotNull works
|
Ashwin Bharadwaj
Greenhorn
Joined: Oct 28, 2010
Posts: 10
|
|
Hi ,
Can anyone please explain me how exactly CheckNotNull works.
Thanks,
Ashwin
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
Ashwin ks wrote:Hi ,
Can anyone please explain me how exactly CheckNotNull works.
Thanks,
Ashwin
Hi Ashwin, Welcome to Java Ranch!
Can you give more context to this? I dont think "CheckNotNull" is from the Java API.
|
Mohamed Sanaulla | My Blog
|
 |
Ashwin Bharadwaj
Greenhorn
Joined: Oct 28, 2010
Posts: 10
|
|
Hi Mohammed,
I m new to java ... i came across a scenario where in the code in if condition check NotNull is used so i want to know how exactly it works.
Thanks for your response Mohammed
Thanks,
Ashwin
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
Ashwin ks wrote:Hi Mohammed,
I m new to java ... i came across a scenario where in the code in if condition check NotNull is used so i want to know how exactly it works.
Can you tell us the reference where you found this usage? Or better post a simple working code. It will help us to clear your doubt easily.
|
 |
Ashwin Bharadwaj
Greenhorn
Joined: Oct 28, 2010
Posts: 10
|
|
if ((user.equals("customer"))) {
selectRadioButton(USERCUSTOMERRADIO);
enterText(USERCUSTOMERTEXTBOX, name);
} else {
selectRadioButton(USERSUPPLIERRADIO);
enterText(USERSUPPLIERTEXTBOX, name);
}
clickElement(CHANGEBUTTON, DEFAULTWAIT);
}
I want know how use not null in these case.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
Ashwin ks wrote:
I want know how use not null in these case.
Please use the [code]tags to post your code.
Am not able to see your Use case here- You want to check if the value is not null? Then you could use- <variable> != null, or to check for empty string you could try- <variable>.equals("").
|
 |
Ashwin Bharadwaj
Greenhorn
Joined: Oct 28, 2010
Posts: 10
|
|
|
OK thanks a lot
|
 |
Jan Hoppmann
Ranch Hand
Joined: Jul 19, 2010
Posts: 99
|
|
mohammed sanaullah wrote:Am not able to see your Use case here- You want to check if the value is not null? Then you could use- <variable> != null, or to check for empty string you could try- <variable>.equals("").
But remember that an empty String ("") is not the same as null.
|
Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9955
|
|
Jan Hoppmann wrote:But remember that an empty String ("") is not the same as null.
This sometimes confuses people. A good way to think of the difference is an empty string is a box with nothing in it. A null string means you don't even have a box.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
fred rosenberger wrote:
This sometimes confuses people. A good way to think of the difference is an empty string is a box with nothing in it. A null string means you don't even have a box.
Nice example!
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
Why are you using such a test in a Listener? That sounds to me like very non-object-oriented programming. You have probably got some poor design there. You can avoid problems with null Strings very easily, by swapping the order of operands, like this:That test cannot suffer a NullPointerException even if user is null.
There are a few instances where a formal null check is required: for example in the old-fashioned use of a FileReader. Note I am showing the correct way to do it with a finally block. Look at this post, and the post farther down where I realised I had made a mistake. That will show you how to check for nullity.
|
 |
Ashwin Bharadwaj
Greenhorn
Joined: Oct 28, 2010
Posts: 10
|
|
|
Thanks to all for the response.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
You're welcome
|
 |
 |
|
|
subject: How CheckNotNull works
|
|
|