Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How to check null of an Object

 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I know how to check Null of an object by using I want a better approach .Here is one sample code snippet :



The above way i can check the null value of objects ...

Any better way by which can check null values would be appreciable....

Thanks,
Satya
 
Marshal
Posts: 27374
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand what you mean by "the null value of objects". In Java an object cannot be null. A variable can contain a null reference, which is what you check with the "= null" comparison.

But you're proposing some kind of code which looks at properties of objects, for some reason. Could you explain what that reason is, or what you're trying to check here?
 
Saloon Keeper
Posts: 9565
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could create an isEmpty() method for each class that checks what ever fields it needs to to determine if it is 'empty' or not. For classes that contain other classes, its isEmpty() method could call the isEmpty() method of the contained class. In this way you would encapsulate the tests.
 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're talking about the chain of nested if statements, take a look at Optional. One implementation of this class is in the JDK for Java 8. If you aren't running Java 8, you can also find other (slightly different) implementations provided by some third party libraries such as Guava. Here's what those ifs might look like with the Java 8 Optional:

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Majumder wrote:Any better way by which can check null values would be appreciable...


I think the great advice so far should help you out there.

My advice is slightly different: Why deal with them at all?

It's tough to know exactly what's going on since you haven't shown us your Client class, but my question is this: Why would a method called getEmployee() not return an Employee?
And if it does return one, why would it not have an Address?

It seems to me that you're dealing with symptoms of a problem instead of tackling the cause, which is that you can have either incomplete or non-existent Employees.

However, if you really can't do anything about it, then another possibility might be to add hasEmployee()/hasAddress() methods, a bit like the way Iterator does it.

I dislike it, for the reason I gave above (it only tackles the symptom), and TBH, if you're on version 8 then Jason's solution is better - but if this code isn't under your control then it's possible that you can't do much about it.

HIH

Winston
 
Marshal
Posts: 8428
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

allen robin wrote:Hey,First you should check if employees itself isn't null, then simply check whether books[i] != null:

allen, please check how to UseCodeTags when you post any code on Ranch. I have added them for you, looks better, isn't?

If you check for books == null and throwing IllegalArgumentException without supplying message, better throw NullPointerException which would be more appropriate. Joshua Bloch in his Effective Java book states, that IllegalArgumentException should be thrown for illegal non null values. Either one or another, string message in it would be nice too.

Code snippet indentation not good enough. Here is Beginning Java forum, so better do not to give OP a feeling, that omitting curly braces is a good thing - which is indeed not, even to there are lots of code in production written like in your example.

 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic