SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:Well, if the result can be null then you should include a null check, or you risk NullPointerExceptions being thrown. You should only skip the null check if a) you're sure the reference cannot be null, or b) you don't care about NullPointerExceptions.
nithin chinni wrote:I am seeing a lot of null check in each an every class for each and every object that is being passed.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Pat Farrell wrote:I believe that the Java original designers were fast and loose with null values. Things like putting null values into lists or sets is just bad. I think the Google Guava folks do a better job. They have made rules, and enforce them.
I use a ton of these in my code, whenever an external user (programmer) could pass in a null argument:
Preconditions.checkNotNull(foo);
You can even give explicit messages using
Preconditions.checkNotNull(foo, "Foo variable is not allowed to be null");
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Dennis Deems wrote:There are plenty of problem spaces where a null value is perfectly legitimate. Maybe I'm a rare bird but pretty much every job I've had has been in such a space. Does our user want our application to crash inexplicably just because they're working with data that wasn't hand-crafted by the developer? Most likely not.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:Is there any way you can prevent nulls remaining in your code?That is prone to NullPointerExceptions (NPE), but allowing nulls into a program is probably the mistake, rather than the NPE itself. In testing, you will know about that early, if you change the constructor to this:That way you can be confident those nulls won’t get into your object in the first place.
Paul Clapham wrote:A lot of people here want to write code so that an IllegalArgumentException will be thrown instead of a NullPointerException. I don't see the benefit of that -- both are unchecked exceptions so they will have the same effect on the calling code.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
nithin chinni wrote:But finally I got the whole point, if the scope of the application is in our hands rather than those calls going to another remote module then it should be fine.
But still the code looks ugly because of lot of if statements.
Winston Gutkowski wrote:I have to disagree there. If the check is for a null parameter in a method, it should most definitely throw an IllegalArgumentException; elsewhere, arguably an IllegalStateException. The only time I expect to see an NPE from my code is when I've made a mistake.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Pat Farrell wrote:
Winston Gutkowski wrote:I have to disagree there. If the check is for a null parameter in a method, it should most definitely throw an IllegalArgumentException; elsewhere, arguably an IllegalStateException. The only time I expect to see an NPE from my code is when I've made a mistake.
You are, of course, entitled to your own opinion. But you are in the minority. Most of the Java code that is in wide-spread use, from Java itself, the Apache and Google libraries, throw a NPE when the argument is null.
Pat Farrell wrote:I most strenuously object to your throwing a IllegalStateException, its not about the state of the object, its about the state of the arguments to the method.
Rob Spoor wrote:There was a suggestion to add support for automatic null checks in Java 7, using question marks. For instance:
If object would be null then s would be null. This hasn't made it into Java 7. Perhaps it will be added to Java 8, but I'm not sure.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Pat Farrell wrote:I most strenuously object to your throwing a IllegalStateException, its not about the state of the object, its about the state of the arguments to the method.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:However, you should never use IllegalStateException like that. You're supposed to throw IllegalStateException when you're calling a method on an object, at a time you're not supposed to call it; meaning, the object is currently in a state where it's illegal to call the method.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:For me the what and why are pretty much always answered by the line number. I don't really need any more context. NPEs are very simple. Something is null, and it shouldn't be. That's why I like them more than IAEs. IAEs could mean all sorts of stuff went wrong. There's nothing ambiguous about an NPE.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:teamsList and its elements are guaranteed to be non-null so you can skip those null checks.
Furthermore, you can omit the if (!listItemStr.equals("")) check since if(listItemStr.startsWith("-")) then !listItemStr.equals("").
You are, of course, entitled to your own opinion. But you are in the minority. Most of the Java code that is in wide-spread use, from Java itself, the Apache and Google libraries, throw a NPE when the argument is null.
In my view, they're the most pernicious (and often the hardest to find) of errors, because they are a discovery, not an actuation, error; and many programmers simply "let them happen" rather than trying to be proactive about them.
IAEs could mean all sorts of stuff went wrong.
Stephan van Hulst wrote:
And no, it's not uncommon for methods to expect arguments that are anything but null. Think of enums.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:This discussion keeps coming up, and like the eternal VI vs Emacs or Eclipse vs Netbeans discussion there will never be a clear winner. So let's stop it right here and now, OK?
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions