The Road to NullPointerException-free Software - with Eclipse JDT

NullPointerException (NPE) is a dominating, omnipresent risk in each and every Java application, and at it's root it is an embarrassingly trivial problem.

Over the years, Eclipse JDT has developed several levels of tool support for statically detecting NPEs at compile time. This tutorial will introduce flow analysis, annotation-based contracts and a null-aware type system as the main tools against NPE.

Additional focus lies on organizational issues facilitating the migration from the risky Java era into an NPE-free software world.


Outline/structure of the Session

The tutorial will consist of four parts, each part starting with a conceptual presentation, moving to practical demonstration, and completed with some exercises for participants. The practical parts of the tutorial will show-case the corresponding tool support provided by Eclipse JDT.

Understanding Flow Analysis

JDT performs intra-procedural flow analysis for detecting possible program executions that would result in NPE. It is good to know some underlying facts to better understand warnings given by the compiler and to learn how to write code, that is better amenable to this analysis.

Using Annotations for Method Contracts

To close the gaps of intra-procedural flow analysis, method contracts describe where null is/is not allowed at entry and exit of any method. JDT supports annotations @NonNull and @Nullable to express such contracts in any program using Java 5 or greater.

Null Type Annotations for a Null-Aware Type System

Starting with Java 8, null annotations can be fully integrated with the type system. In this approach each typed expression in a program also carries the information whether or not null is possible. This is particularly relevant for code using Java generics. In the end this enables a compiler to detect every possible NPE in a program.

Blessing Legacy Libraries with External Annotations

A major obstacle towards fully null-checked programs is the fact that existing Java libraries do not specify null contracts. In this light, most existing libraries can be regarded as legacy with respect to null checking. Rather than waiting for everybody else to update their libraries with null annotations, users of any library can externally attach null annotations to the signatures of any library class and method.

Learning Outcome

After attending this tutorial you won't have any good excuse left for programming any new NullPointerException.

You will know the levels of analysis done by the JDT compiler and you will know how to configure your projects to leverage the various stages of this capability.

You will also learn how to create NPE-free software when starting your next project. For existing software you will learn essential strategies for incrementally removing the risk of NPE, method-by-method, class-by-class and package-by-package.

In the end you will enjoy a way of programming, where writing new functionality is not disturbed by the fear of NPE, knowing that the compiler will remind you of any necessary clean-up before you ship your software.

Target Audience

java developers


Participants should come with a computer on which Eclipse Neon is installed - any package containing JDT and the Eclipse Git Team Provider will suffice.

Exercises in the tutorial will be based on source code shared as a git repository. Please check back here, later, for download URL of that repository.

schedule Submitted 9 months ago

Comments Subscribe to Comments

comment Comment on this Proposal

  • Liked Stephan Herrmann

    Runtime Specialization - Java has never been so dynamic before

    Stephan Herrmann
    Stephan Herrmann
    schedule 9 months ago
    Sold Out!
    45 mins

    Much of software development is about creating variants of things that have been done in similar ways before. Typical means to create such variants include inheritance / subtyping, extension points and dependency injection. Unfortunately, all of these approaches require that the original author anticipated the required variation points in his/her module, to enable you to create whatever variant comes to your mind. This means, modules intended for reuse tend to be bloated with indirections just in case someone needs to modify something, and still many interesting adaptations are simply not possible due to lack of pre-planning.

    Object Teams reverses this situation. Creating unanticipated variants is at the core of this approach. Starting with the Neon release, even a running application can be morphed into a new variant of itself.

  • Neeraj Bhusare
    Neeraj Bhusare
    schedule 9 months ago
    Sold Out!
    45 mins

    Xtend is a fully-featured Java like language that is tightly integrated with Java. It has a more concise, readable and expressive syntax than Java, and provides additional features such as Type inference, Extension methods and Lambda expressions. It comes with state of the art IDE support for Eclipse and IntelliJ IDEA. It also provides interface for integration of text editors for your language in Web applications.

    Xtend itself is implemented in Xtext and it is a proof of concept of how involved a language implemented in Xtext can be.

    The objective is to introduce the audience to Xtend language and how it can be used to write cleaner code against any Java APIs and solve day-to-day challenges in much better ways.