wood burning stoves 2.0*
The moose likes Java in General and the fly likes check Input array null or empty Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "check Input array null or empty" Watch "check Input array null or empty" New topic
Author

check Input array null or empty

Steve Jiang
Ranch Hand

Joined: May 17, 2004
Posts: 107
If the input parameter is null or empty, what should we do to handle it?

like the following code to get the min number from array , Shoud it return error code, or throw excpetion? or should it be handled before the arr is passed in function?

Also what is the normal way to set for the intial value for min at first?

Thanks,



[ January 18, 2008: Message edited by: Steve Jiang ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Integer.MAX_VALUE is the largest possible int; that's what I would use for the initial value of "min".

What should this method do if the array is null? There are two possibilities: either return a sensible result (Integer.MAX_VALUE works for me), or throw an exception (java.lang.IllegalArgumentException is a good choice). Returning a default value is convenient, but it could mask a bug elsewhere in the program if they array shouldn't be null. I'd be more likely to write the exception-throwing one, especially if this method is intended to be widely used.

What should it do if the array is zero-length? A similar argument applies, but here I might be more likely to return the default value. There might be perfectly good reasons for there to be a list of zero numbers.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Sounds more like a beginner's question.

You shouldn't allow them to pass a null input. Suggest adding this at the beginning of the method:To deal with zero-length arrays, you can either let the method throw the standard ArrayIndexOutOfBoundsException or an IllegalArgumentException as shown above.
Why are you using a complicated shift operation when you can get the absolute minimum value from a static variable in the Integer class? Go and look through the API for it.
Actually there is an easier way to get a starting value for the minimum of an array: use its very first member. That is of course the lowest value you have encountered so far. Then alter your for loop to start for(int i = 1;

Think about it; it would work for a max method too.

[edit]A few minor spelling errors[/edit]
[ January 19, 2008: Message edited by: Campbell Ritchie ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
. . . and work out whether you prefer EFH's approach to zero-length arrays (he is correct, there are legitimate reasons for having zero-length arrays), or mine.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

I also would suggest throwing back a NullPointerException in case of the input array being null, which would be more meaningful.

Or as you asked, you can set a specific error code for the array being null and throw it back.


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]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Thank you, Raghavan Muthu.

But I still prefer the notion of throwing an Exception if null is passed.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
If it an error for the array to be null, then I agree that an exception should be thrown.

If you are in control of the client, then change it to always pass a valid array value. Then the findMin(...) method need not check for null.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Steve Jiang
Ranch Hand

Joined: May 17, 2004
Posts: 107
Thanks for all comments!

I just wondering that NullPointerException is a RunTimeException, some book memtion that developer don't need to check the RuntimeException.

In another word, if we don't add the NullPointerException, at the runtime, if the input array is null, JVM will throw NullPointerException automatically,"Exception in thread "main" java.lang.NullPointerException"

I am not sure if we need to describe exception explictly here. It should be better, but is it required for offical coding, or what is the normal style in real coding?

For empty array, it is better to add some special checking like an exception, default value, or ERROR code.
[ January 19, 2008: Message edited by: Steve Jiang ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Steve Jiang:
..
I just wondering that NullPointerException is a RunTimeException,


Yes, it is.


some book memtion that developer don't need to check the RuntimeException.


Yes, but not always. As it is an unchecked exception, the compiler does NOT insist you to catch this exception. But, checking is a must so as to avoid the unexpected and abrupt termination of the program.

The books might have suggested NOT to catch and i think you might have got confused with "Catching Vs Checking".


In another word, if we don't add the NullPointerException, at the runtime, if the input array is null, JVM will throw NullPointerException automatically,"Exception in thread "main" java.lang.NullPointerException"


You have told very clearly through the words "don't add the NullPointerException" -- that is for adding a catch block!

Though it is true, you do NOT need to add a block for that. Moreover you do NOT need to have it in throws clause as well.

It is always better to ensure not to let the runtime environment blow up!


I am not sure if we need to describe exception explictly here. It should be better, but is it required for offical coding, or what is the normal style in real coding?


As it comes in an unchecked and runtime exception category type, the general practice is NOT to. You can handle it by "checking" it.


For empty array, it is better to add some special checking like an exception, default value, or ERROR code.


Yes, that sounds good. You can go according to what your requirement says although the options given by you sound reasonable.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Notice that whoever first mentioned Exceptions on this thread took the unusual step of adding JavaDoc documentation to the snippets posted, so as to make sure the users knows about the RuntimeExceptions. And took it for granted that people on the intermediate thread knew about RuntimeExceptions.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Campbell Ritchie:
Notice that whoever first mentioned Exceptions on this thread took the unusual step of adding JavaDoc documentation to the snippets posted, so as to make sure the users knows about the RuntimeExceptions. And took it for granted that people on the intermediate thread knew about RuntimeExceptions.


CR, i don't get anything from what you said!

It looks the same person who initiated the thread and spoke about exceptions he only mentioned about RuntimeException as well.

Can you please be a bit more specific? (if it not bothering you too much)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
All right, then.


I first mentioned Exceptions on this thread. Notice that when I did so, I made sure to add documentation comments to the code which said which Exceptions might be thrown.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

CR,

Sorry to say that still i am unable to get what you intended to say in your last but one post! It is okay and let us leave that. I don't think it adds some value to the OP.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Notice that I was the first person to introduce Exceptions into the discussion; I made sure to prefix the code snippet with documentation comments to make sure that anybody using this method would know it was liable to throw the Exceptions. You raised two possibilities, that we throw an Exception or return a "dummy" value for a null input. I aid that the Exception would have been better; people on this thread are disagreeing about whether to throw an Exception or return a "dummy" value, which out to be MAX_VALUE, not 0, for a 0-length array. The way the original poster asked the question, I thought he meant that a 0-length array was regarded as an error, but EFH thought a 0-length array might be a legitimate input.

I think we both think the same thing, but are expressing it differently.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I think the confusion stems from the fact that you were not the first person to talk about exceptions in this thread. Both Steve and EFH did before you.


"I'm not back." - Bill Harding, Twister
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
You are right. I am very sorry.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: check Input array null or empty