• Post Reply Bookmark Topic Watch Topic
  • New Topic

How CheckNotNull works  RSS feed

 
Ashwin Bharadwaj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Can anyone please explain me how exactly CheckNotNull works.

Thanks,
Ashwin
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashwin Bharadwaj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK thanks a lot
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for the response.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!