IllegalArgumentException or NullPointerException for a null parameter? [closed]

I have a simple setter method for a property and null is not appropriate for this particular property. I have always been torn in this situation: should I throw an IllegalArgumentException, or a NullPointerException? From the javadocs, both seem appropriate. Is there some kind of an understood standard? Or is this just one of those things that you should do whatever you prefer and both are really correct?

26 s
26

It seems like an IllegalArgumentException is called for if you don’t want null to be an allowed value, and the NullPointerException would be thrown if you were trying to use a variable that turns out to be null.

Leave a Comment