aspose file tools*
The moose likes Beginning Java and the fly likes thou shalt never be equal to null? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "thou shalt never be equal to null?" Watch "thou shalt never be equal to null?" New topic
Author

thou shalt never be equal to null?

Fabio Fonseca
Greenhorn

Joined: Jan 19, 2006
Posts: 29
Hello guys,

Is it possible to check if something is equal to null? And receive a boolean answer? I'm pretty confused with this one, why I can have a value of null for an object and then right after can't I do boolean stuff on it?

please check what I did:

String userConfirmation = users.get(0).getConfirmationEmail();

---> It is an object that Hibernate gave me. It is a MySQL field which started with a null value. hibernate created this mysql field, therefore, its initial value is set (?) to null.

logger.info("confirmation email = " + userConfirmation);
logger.info("confirmation email = " + userConfirmation.equals(null));
logger.info("confirmation email = " + userConfirmation.equals(""));
logger.info("confirmation email = " + userConfirmation.equals("OK"));

----> What I received with the original field set to null:

1. mysql field equals null:

confirmation email = null
confirmation email = logic error

---> field set to empty string

2. mysql field equals empty string:

confirmation email =
confirmation email = false
confirmation email = true
confirmation email = false

---> field set to OK

3. mysql field equals OK:

confirmation email = OK
confirmation email = false
confirmation email = false
confirmation email = true


I know that I can update the field by hand, setting it all to empty string in mysql. But I want to understand why "equals(null)" doesn't works..

Thanks!

p.s. is this suited for this forum? have just read that "questions of any difficulty should be posted in the intermediate forum".. Moderators, please move my post if you think it is no suited for this forum.
thanks!
[ April 05, 2006: Message edited by: Fabio Fonseca ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You can test to see if a reference is pointing to null with reference == null
Fabio Fonseca
Greenhorn

Joined: Jan 19, 2006
Posts: 29
Originally posted by Keith Lynn:
You can test to see if a reference is pointing to null with reference == null


Ermmm,

Keith, how do I actually do that?

thanks!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

Originally posted by Fabio Fonseca:


Ermmm,

Keith, how do I actually do that?

thanks!


Either ...



or...



will work.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thou shalt never be equal to null?