{"id":9,"date":"2020-12-14T21:27:26","date_gmt":"2020-12-14T21:27:26","guid":{"rendered":"http:\/\/victorrentea.ro\/blog\/?p=9"},"modified":"2020-12-16T06:33:43","modified_gmt":"2020-12-16T06:33:43","slug":"avoiding-null-pointer-exception","status":"publish","type":"post","link":"https:\/\/victorrentea.ro\/blog\/avoiding-null-pointer-exception\/","title":{"rendered":"Avoiding NullPointerException"},"content":{"rendered":"<div id=\"bsf_rt_marker\"><\/div><p>The terrible <code>NullPointerException<\/code> (NPE in short) is the most frequent Java exception occurring in production, acording to <a href=\"https:\/\/www.overops.com\/blog\/the-top-10-exceptions-types-in-production-java-applications-based-on-1b-events\/\">a 2016 study<\/a>. In this article we\u2019ll explore the main techniques to fight it: the self-validating model and the <code>Optional<\/code> wrapper.<\/p>\n<h2>Self-Validating Model<\/h2>\n<p>Imagine a business rule: every Customer has to have a birth date set. There are a number of ways to implement this constraint: validating the user data on the create and update use-cases, enforcing it via <code>NOT NULL<\/code> database constraint and\/or implementing the null-check right in the constructor of the Customer entity. In this article we&#8217;ll explore the last one.<\/p>\n<p>Here are the top most used forms to null-check in constructor today:<\/p>\n<pre><code class=\"language-java\">public Customer(@NonNull Date birthDate) { \/\/ 3\n  assert (birthDate != null); \/\/ 4\n  if (birthDate == null) { \/\/ 1\n     throw new IllegalArgumentException();\n  }\n  this.birthDate = Objects.requireNonNull(birthDate); \/\/ 2\n}<\/code><\/pre>\n<p>The code above contains 4 <em>alternative<\/em> ways to do the same thing, any single one is of course enough:<\/p>\n<ol>\n<li>Classic <code>if<\/code> check<\/li>\n<li>One-liner check using Java 8 <code>java.util.Objects<\/code> &#8211; most widely used in projects under development today<\/li>\n<li>Lombok <code>@NonNull<\/code> causing an <code>if<\/code> check to be added to the generated bytecode.<\/li>\n<li>assert keyword: I personally don&#8217;t like it because assertions can be disabled globally via a JVM argument<\/li>\n<\/ol>\n<p>If there is a setter for birth date, the check will move there, leaving the constructor to call that setter. <\/p>\n<p>Enforcing the null-check in the constructor of your data objects has obvious advantages: no one could ever forget to do it. However, frameworks writing directly to the fields of the instance via reflection may bypass this check. Hibernate does this by default, so my advice is to also mark the corresponding required columns as <code>NOT NULL<\/code> in the database to make sure the data comes back consistent. Unfortunately, the problem can get more complicated in legacy systems that have to tolerate &#8216;incorrect historical data\u2019.<\/p>\n<blockquote>\n<p><strong>Trick<\/strong>: Hibernate requires a no-arg constructor on every persistent entity, but that constructor can be marked as <code>protected<\/code> to hide it from developers. <\/p>\n<\/blockquote>\n<p>The moment all your data objects enforce the validity of their state internally, you do get a better night sleep, but there&#8217;s also a price to pay: creating dummy incomplete instances in tests becomes impossible. The typical tradeoff is relying more on <a href=\"https:\/\/martinfowler.com\/bliki\/ObjectMother.html\">Object Mother<\/a>s for building valid test objects.<\/p>\n<p>So, in general, whenever a null represents a data inconsistency case, throw exception as early as possible.<\/p>\n<p>But what if that <code>null<\/code> is indeed a valid value? For example, imagine our Customer might not have a Member Card because she didn&#8217;t yet create one or maybe she didn&#8217;t want to sign up for a member card. We&#8217;ll discuss this case in the following section.<\/p>\n<h2>Getters returning Optional<\/h2>\n<blockquote>\n<p><strong>Best-practice<\/strong>: Since Java 8, whenever a function needs to return <code>null<\/code>, it should declare to return <code>Optional<\/code> instead<\/p>\n<\/blockquote>\n<p>Developers rapidly adopted this practice for functions computing a value or fetching remote data. Unfortunately, that didn&#8217;t help with the main source of NPEs: our entity model.<\/p>\n<blockquote>\n<p>A getter for a field which may be <code>null<\/code> should return <code>Optional<\/code>.<\/p>\n<\/blockquote>\n<p>Assuming we&#8217;re talking about an Entity mapped to a relational database, then if you didn&#8217;t enforce <code>NOT NULL<\/code> on the corresponding column, the getter for that field should return <code>Optional<\/code>. For non-persistent data objects or NoSQL datastores that don&#8217;t offer <code>null<\/code> protection, the previous section provides ideas on how enforce null-checks programmatically in the entity code. <\/p>\n<p>This change might seem frightening at first because we\u2019re touching the &#8216;sacred&#8217; getter we are all so familiar with. Indeed, changing a getter in a large codebase may impact up to dozens of places, but to ease the transition you could use the following sequence of safe steps: <\/p>\n<ol>\n<li>\n<p>Create a second getter returning <code>Optional<\/code>:<\/p>\n<pre><code class=\"language-java\">public Optional<String> getMemberCardOpt() {\n  return Optional.ofNullable(memberCard);\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>Change the original getter to delegate to the new one:<\/p>\n<pre><code class=\"language-java\">public String getMemberCard() {\n  return getMemberCardOpt().orElse(null);\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>Make sure all the Java projects using the owner class are loaded in your workspace.<\/p>\n<\/li>\n<li>\n<p><strong>Inline<\/strong> the original getter everywhere. Everyone will end up calling <code>getMemberCardOpt()<\/code>.<\/p>\n<\/li>\n<li>\n<p><strong>Rename<\/strong> the getter to the default name (removing the <code>Opt<\/code> suffix)<\/p>\n<\/li>\n<\/ol>\n<p>You&#8217;re done, with zero compilation failures. After you do this, everyone previously calling the getter will now do <code>getMemberCard().orElse(null);<\/code>. In some cases this might be the right thing to do, as in: <code>dto.phone=customer.getPhone().orElse(null);<\/code><\/p>\n<p>But let&#8217;s suppose you wanted to use a property of the MemberCard, and you were careful to check for <code>null<\/code>:<\/p>\n<pre><code class=\"language-java\">if (customer.getMemeberCard() != null) { \/\/ Line X\n    applyDiscount(order, customer.getMemeberCard().getPoints());\n}<\/code><\/pre>\n<p>After applying the refactoring steps above, the code gets refactored to <\/p>\n<pre><code class=\"language-java\">if (customer.getMemeberCard().orElse(null) != null) { \/\/ Line X\n    applyDiscount(order, customer.getMemeberCard().orElse(null).getPoints());\n}<\/code><\/pre>\n<p>The <code>if<\/code> condition can be simplified by using <code>.isPresent()<\/code> and the second line by using <code>.get()<\/code>. Then one could even shorten the code to a single line:<\/p>\n<pre><code class=\"language-java\">customer.getMemberCard().ifPresent(card -&gt; applyDiscount(order, card.getPoints()));<\/code><\/pre>\n<p>This means that you still need to go through all the places the getter is called to <em>improve<\/em> the code as we saw above. Furthermore, I bet that in large codebases you&#8217;ll also discover places where the null-check (\/\/ Line X) was forgotten because the developer was tired\/careless\/rushing back then:<\/p>\n<pre><code>applyDiscount(order, customer.getMemeberCard().orElse(null).getPoints());<\/code><\/pre>\n<p><strong>Tip<\/strong>: IntelliJ will hint you about the possible NPE in this case, so make sure the inspection &#8216;Constant conditions and exceptions&#8217; is turned on. <\/p>\n<p>Signaling the caller at compile-time that there might be nothing returned to her is an extremely powerful technique that can defeat the most frequent bug in Java applications. Most NPEs occur in large projects mainly because developers aren\u2019t fully aware some parts of the data might be <code>null<\/code>. It happened on our project: we discovered dozens of <code>NullPointerExcepton<\/code>s just waiting to happen when we moved to <code>Optional<\/code> getters.<\/p>\n<h3>Frameworks and Optional getters<\/h3>\n<p>Would frameworks allow getters to return Optional?<\/p>\n<p>First of all, to make it clear, we only changed the return type of the getter. The setter and the field type kept using the raw type (not <code>Optional<\/code>). <\/p>\n<p>All modern object-mapper frameworks (eg Hibernate, Mongo, Cassandra, Jackson, JAXB &#8230;) can be instructed to read from private fields via reflection (Hibernated does it by default). So really, the frameworks don\u2019t care about your getters. <\/p>\n<h3>When is Optional overkill?<\/h3>\n<p>You should consider making null-safe the objects you write logic on: Entities and Value Objects. As I explained in my <a href=\"https:\/\/www.youtube.com\/watch?v=tMHO7_RLxgQ&amp;list=PLggcOULvfLL_MfFS_O0MKQ5W_6oWWbIw5&amp;index=3\">Clean Architecture talk<\/a>, you should avoid writing logic on API data objects (aka Data Transfer Objects). Since no logic uses them, null-protection is overkill. <\/p>\n<blockquote>\n<p>Use <code>Optional<\/code> in your Domain Model not in your DTO\/API Model.<\/p>\n<\/blockquote>\n<h2>Pre-instantiate sub-structures<\/h2>\n<p>Never do this:<\/p>\n<pre><code class=\"language-java\">private List&lt;String&gt; labels;<\/code><\/pre>\n<blockquote>\n<p>Always initialize the collection fields with an empty one!<\/p>\n<\/blockquote>\n<pre><code class=\"language-java\">private List&lt;String&gt; labels = new ArrayList&lt;&gt;();<\/code><\/pre>\n<p>Those few bytes allocated beforehand almost never matter. On the other hand, the risk for doing <code>.add<\/code> on a <code>null<\/code> list is just to dangerous. In some other cases you might want to make the field <code>final<\/code> and take it via the constructor. Never leave collections references to have a <code>null<\/code> value.<\/p>\n<p>Many teams choose to decompose larger entities into smaller parts. When those parts are mutable, make sure you instantiate the parts in the parent entity:<\/p>\n<pre><code class=\"language-java\">private BillingInfo billingInfo = new BillingInfo();<\/code><\/pre>\n<p>This would allow the users of your model to do <code>e.getBillingInfo().setCity(city);<\/code> without worrying about nulls.<\/p>\n<h2>Conclusion<\/h2>\n<p>You should consider upgrading your entity model to either reject a <code>null<\/code> via self-validation or present the nullable field via a getter that returns <code>Optional<\/code>. The effort of changing the getters of the core entities in your app is considerable, but along the way, you may find many dormant NPEs. <\/p>\n<p>Lastly, always instantiate embedded collections or composite structures.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The terrible NullPointerException (NPE in short) is the most frequent Java exception occurring in production, acording to a 2016 study. In this article we\u2019ll explore the main techniques to fight it: the self-validating model and &hellip; <\/p>\n","protected":false},"author":1,"featured_media":10,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2,6,3,4],"class_list":["post-9","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-practical-tips","tag-clean-code","tag-exceptions","tag-lombok","tag-refactoring"],"_links":{"self":[{"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":7,"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":93,"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/posts\/9\/revisions\/93"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/media\/10"}],"wp:attachment":[{"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/victorrentea.ro\/blog\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}