Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 9
    Actually, your example constitutes code bloat: the first line is superfluous because the NPE would be thrown in the second line. ;-) Commented Mar 7, 2013 at 1:21
  • 1
    True. A better example would be if the second line were doCalc(someObject). Commented Mar 7, 2013 at 5:23
  • Depends. If you are the author of doCalc(), I'd suggest putting the check into that method's body (if possible). And then you most likely will call someObject.someMethod() where again there is no need to check for null. :-) Commented Mar 7, 2013 at 7:07
  • Well, if you are not the author of doCalc(), and it doesn't immediately throw NPE when given null, you'd need to check for null and throw NPE yourself. That's what Objects.requireNonNull() is for. Commented Mar 9, 2013 at 6:43
  • 7
    It's not just code bloat. Better to check up front than halfway through a method that causes side effects or uses time/space. Commented Sep 26, 2013 at 11:33