Null Pointer Exception

Hi,

I'am new to Java and OOPS and I would like to know the best pratices programmers follow in the following situation:

I have a method defined as

public String concatenate(String argument1, String argument2){

//Method defined here

}

Ideally my method does not expect any argument as null.

My question is :

1. Should my method handle null objects gracefully? (I beleive it should otherwise it may throw a NullPointerException...)

2. If any of the arguments is null, what should it do. Should it return back a null object or should it throw a application-specific user-defined Exception?

3. What if my method does not return anything; return type is void? How will it tell the invoking entity that this method has not been executed due to error condition.

4. Should such null-checks be made in private methods also.

I fully understand this is not a Java specific query. Its like there are so many people logged in this site that I do not really understand where else I will get a prompt and precise reply.

Thanks in advance.

Jay

[1121 byte] By [Jay@Javaa] at [2007-10-2 22:01:41]
# 1

> Hi,

>

> I'am new to Java and OOPS and I would like to know

> the best pratices programmers follow in the following

> situation:

>

> I have a method defined as

>

> public String concatenate(String argument1, String

> argument2){

>//Method defined here

> Ideally my method does not expect any argument as

> null.

>

>

> My question is :

> 1. Should my method handle null objects gracefully?

> (I beleive it should otherwise it may throw a

> NullPointerException...)

> 2. If any of the arguments is null, what should it

> do. Should it return back a null object or should it

> throw a application-specific user-defined Exception?

>

> 3. What if my method does not return anything; return

> type is void? How will it tell the invoking entity

> that this method has not been executed due to error

> condition.

surround the call to the method with a try/catch, and inside the method throw an exception if an error happens.

> 4. Should such null-checks be made in private methods

> also.

>

> I fully understand this is not a Java specific query.

> Its like there are so many people logged in this site

> that I do not really understand where else I will get

> a prompt and precise reply.

>

> Thanks in advance.

> Jay

OniShiroa at 2007-7-14 1:17:58 > top of Java-index,Java Essentials,New To Java...
# 2

> 1. Should my method handle null objects gracefully?

Always - (Note, I'm not specifiying what gracefully is though)

> 2. If any of the arguments is null, what should it

> do. Should it return back a null object or should it

> throw a application-specific user-defined Exception?

This depends on how your method is used.

> 3. What if my method does not return anything; return

> type is void? How will it tell the invoking entity

> that this method has not been executed due to error

> condition.

See the answer given by the other poster

> 4. Should such null-checks be made in private methods

> also.

Depends on how they are used.

macrules2a at 2007-7-14 1:17:58 > top of Java-index,Java Essentials,New To Java...
# 3

I think your question is both relevent to this forum and an excelent example of someone who is asking the right question to become and good OO programmer.

> My question is :

> 1. Should my method handle null objects gracefully?

> (I beleive it should otherwise it may throw a

> NullPointerException...)

As already mentioned you should always handle exceptions gracfully, but that depends what gracful is in the perticular situation. You have to decide whether a null for one of the values is appropriate for your method.

It may be the case that you have a set method that allows you to set a value to null. This may be a desired behaviour. If however your method cannot perform it's intended purpouse with the value provided, you should provide feedback to the calling method, using exceptions.

> 2. If any of the arguments is null, what should it

> do. Should it return back a null object or should it

> throw a application-specific user-defined Exception?

>

Again, it depends on your method. It may be that returning null instead of an object is appropriate. if you want to get a value that hasn't been set yet, maybe null is appropriate, or maye an exception is more appropriate, it is up to you to decide. A general rule you can apply to help you decide is: "has the method performed the task it is suppost to": yes? return the null value - no? throw an exception

> 3. What if my method does not return anything; return

> type is void? How will it tell the invoking entity

> that this method has not been executed due to error

> condition.

Exceptions are not returns. An exception can be thrown from a void method in exactly that same way as a non-void method. Exceptions should always be used to privide feedback of errounouse behaviour or failures. It is not the job of any return value to carry faliure feedback information.

> 4. Should such null-checks be made in private methods

> also.

>

This is a good question. I would say, if you don't have 100% control over weither the value is null and a null value could cause the method to fail, then putting full exception handelling into your private methods is a good idea. Also if the class is expected to grow very large and/or will have multiple people working on it, then it is always better safe than sorry.

As a final note I would like to add that quite oftern nulls are valid states for refrences to objects, and in many cases a null can be as useful as the object it's self.

Ken_Sa at 2007-7-14 1:17:58 > top of Java-index,Java Essentials,New To Java...