validate date not null and throw exception

isiauna2009
isiauna2009 used Ask the Experts™
on
Hello Experts,
I am totally lost. I am trying to validate a perameter which is (GregorianCalender, date ) the validation must not be null.
I was using an if statement and used (date != null). I have to throw an illegalArugmrnt and I used  {
         IllegalArgumentException exception = new IllegalArgumentException ();
            throw exception;
        }
My test keeps failing, it is my code. How can I Validate that the given parameter date is not null.

What am I doing wrong?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
if (date!=null) {
   throw new IllegalArgumentException("Date cannot be null");
}

Commented:
Probably need a bit more of your code.  To see if a parameter is not null is what you've got.  You can see if (object == null) and handle it that way or check to make sure its != null.
Shouldn't you be saying:

if (date == null) {
         IllegalArgumentException exception = new IllegalArgumentException ("Your Message");
            throw exception;
        }

Instead of:

if (date != null) {
         IllegalArgumentException exception = new IllegalArgumentException ("Your Message");
            throw exception;
        }


OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Top Expert 2016

Commented:
>>
Shouldn't you be saying:
..

Instead of:
>>

Absolutely ;)
Top Expert 2016

Commented:
There's actually no reason you need to catch it in a real program. Passing null would be an error and runtime would deal with it.
Mick BarryJava Developer
Top Expert 2010

Commented:
> Shouldn't you be saying:

good pickup :)
I better get some coffee
Hm, interesting discussion :-)
May be you have catched the exception in the same method, instead of declare the method in this way:
...validate(Date dt) throws IllegalArgumentException {
Top Expert 2016

Commented:
You might also like to throw for dates that are out of range

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial