r/java • • Jun 27 '16

Null pointer exceptions hell

http://dobegin.com/npe-hell/
0 Upvotes

18 comments sorted by

View all comments

-4

u/yogazook Jun 28 '16

And yet they're the easiest exceptions to avoid. It's actually pretty much programming 101: 1) don't return null, and/or 2) check to make sure it's not null before you do something that can throw an NPE.

var element = null;
element.getAttribute("id");
    // crash

Yeah, no shit.

1

u/QshelTier Jun 30 '16

Checking for null is okay (well, not really, it sucks) but having a compiler that refuses to create a binary if you forget it is better.