File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes [Newbie] What'is the best practice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[Newbie] What Watch "[Newbie] What New topic
Author

[Newbie] What'is the best practice?

Max Bean
Ranch Hand

Joined: Mar 09, 2006
Posts: 31
If I wanna write the method which return any kind of Object, what's the cleaner and most efficient code to check the initial state and check whether it's null or not?

Another thing is the invoker. If I can guarantee that method will return at least null to the invoker, how can I protect NullPointerException in the point of view of the invoker?

Please do not consider this question as stupidity. I'm really noobiest...
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10


Joanne
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Maximilian Boonyoung:

If I wanna write the method which return any kind of Object,
what's the cleaner and most efficient code to check the initial state and check whether it's null or not?



For which you can have a code snippet like this.




Originally posted by Maximilian Boonyoung:

Another thing is the invoker. If I can guarantee that method will return
at least null to the invoker, how can I protect NullPointerException in
the point of view of the invoker?



As you know the method could return *any kinda* object, you should
typecast the returned object before its first use.

Let's say you have an object being returned of type MyClassObj. (It
can be of any other object like String, ArrayList etc).




Does this help?
[ August 17, 2007: Message edited by: Raghavan Muthu ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If that all worked for you, let's take another step and talk about why you want to return null. It's a pain for the caller to have to test and it's often unclear exactly what we're trying to tell somebody when we return null. We have ways of making life easier for the caller in almost any program.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Maximilian Boonyoung:
... Please do not consider this question as stupidity. I'm really noobiest...

I think it's a great question! In fact, it turns out there can be big differences of opinion about whether returning null is a good idea.

How this is handled depends a lot on the context of the problem. So as Stan suggested, "let's take another step and talk about why you want to return null."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by marc weber:

I think it's a great question! In fact, it turns out there can be big differences of opinion about whether returning null is a good idea.



Yes, very much. I agree with Marc.

This makes us think on taking a decision should we really need to return null?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Newbie] What'is the best practice?
 
Similar Threads
String class methods question
When (and when not ) to use a try/catch block?
nx:All of URLy Bird 1.1.3 find and search method
struts validator
Can getMessage() method of Exception class return Null?