Idea logo

The IntelliJ IDEA Blog

The Capable & Ergonomic Java IDE by JetBrains

Tips & Tricks

Detecting probable NPE’s

It is a well-known fact that IntelliJ IDEA provides developers with a huge number of automated code inspections and quick fixes for discovering and correcting a lot of real and potential problems.

One of them is called “Constant conditions & exception” and is capable of detecting expressions that are always true or false (potential dead code) as well as points out where a RuntimeException may be thrown.

IntelliJ IDEA designed two annotations that make this inspection even more powerful. These annotations allow you to specify method contracts formally, and then validate whether these contracts are actually followed.
The @Nullable annotation reminds you on necessity to introduce a null value check when:

  • calling methods that can return null
  • dereferencing variables (fields, local variables, parameters) that can be null

The @NotNull annotation is, actually, an explicit contrast declaring the following:

  • a method should not return null
  • a variable (like fields, local variables, and parameters) cannot hold the null value

For instance, let’s create a method where the parameter has the @NotNull annotation. Then call this method with a parameter that potentially can be null.

public class TestNullable {
  public void foo(@NotNull Object param) {
   int i = param.hashCode();
   //some code here
  public void callingNotNullMethod(ArrayList list) {
   //some code here
   if (list == null) otherMethod(list);

IntelliJ IDEA highlights the problems “on-the-fly”, so you can see the inspection results right in the editor.
Get interested? Learn more about @Nullable and @NotNull annotations in IntelliJ IDEA in Nullable How-To .

Note   This tip was originally posted at

Comments below can no longer be edited.

2 Responses to Detecting probable NPE’s

  1. t800t8 says:

    September 4, 2006

    Now we have it (and more):

  2. Mihai says:

    August 18, 2016

    It would be really useful if this were added to ActionScript / Flex development in IntelliJ!

Discover more