@Nullable : Java Glossary

@Nullable
I have had only very short time experimenting with @Nullable and @NotNull. This is my best understanding so far. This information should not yet be relied on. I post it now mainly hoping people who know much more than I do will offer correction.
Intellij has a design-by-contract annotation to verify that you are handling nulls correctly. You can mark parameters, returns, or variables as either @Nullable, meaning that can legitimately be null, or @NotNull where they may not be null.

The most common convention is you describe returns by putting the @Nullable/@NotNull before everything in the method declaration, e.g. before the public. You describe parameters by putting the @Nullable/@NotNull before everything in the parameter e.g. before the final.

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
...
// placementof @Nullable and @NotNull
@NotNull private String expand( @NotNull File targetFile,
                               @Nullable String image)
                              {
                              ...
                              }

If your code violates the annotations, you will be warned to add specific code for null checking, possibly an assert or if ( var != null ) perhaps overly conservatively. If you don’t fix it, the code will still run, but could possibly throw a NullPointerException.

It is not as though you have to write more code than you would normally (other than the annotations themselves). The annotations just remind you about code you forgot to write.

If you am passing in a value to one method that requires � @NotNull parameters, and that value was obtained by another method whose return value is marked @NotNull, you need not write an explicit null check. That saves you coding and reduces the need for overly conservative defensive programming.

Of course you are not obligated to mark every return, parameter and variable with an annotation. The unmarked ones you deal with at run time with a NullPointerException and/or with explicit null-checking code.

This sounds simple enough but there are quite a number of steps and complications to getting it to work:

  1. The @Nullable and @NotNull annotations are an open source feature of the IntelliJ IDE (Integrated Development Environment), not a feature of the Java language. Annotations in general are of course built into Java. JetBrains have offered them to Oracle to include in standard Java, but Oracle has not yet accepted them.
  2. You have to configure the feature to work in IntelliJ. You must configure J:\Program Files (x86)\JetBrains\IntelliJ IDEA 10.5.2\redist\annotations.jar as one of the libraries accessible to your module. This will only apply to your 1.5+ targets.
    1. In the project panel, click the name of your project. This is the tricky part. You must click the project, not the module to configure the module.
    2. Click Module Settings.
    3. Click Project.
    4. On the left, click modules.
    5. Click the name of one of your modules from the middle column.
    6. Click Dependencies
    7. Click add.
    8. Click Single-entry module library
    9. Navigate to J:\Program Files (x86)\JetBrains\IntelliJ IDEA 10.5.2\redist\annotations.jar
    10. Click OK.
    11. Click OK.
  3. Strange as it sounds to the novice programmer, but it is also possible to store your annotations separately from the source code, though the IntelliJ IDE will display them as if they were embedded. This ability in not peculiar to IntelliJ. log4j,� SLF4J, XStream, Spring, mysql-connector-j, commons-lang, junit… all store information about he code separately from the source code, many using the annotation mechanism. JSR-305 on Annotations for Software Defect Detection is nothing but annotation.
  4. To make the annotations available to your JDK (Java Development Kit) used by IntelliJ to compile your code (as distinct from the JRE (Java Runtime Environment) it uses to run the IDE), or to let ANT (A Neat Tool) compile outside IntelliJ, you will need to install annotations.jar in your own various ext directories. Theoretically you don’t need to put in all of them, but I find it simpler to do so.
    where to look for ext directories :
  5. At this point all will work fine, except that if you reorganize your imports the following lines will disappear:

    // Disappearing imports for @NotNull and @Nullable
    import org.jetbrains.annotations.NotNull;
    import org.jetbrains.annotations.Nullable

    What you have to do now is configure the auto-import function to ignore these imports:

    1. Click File.
    2. Click Settings.
    3. Click +Editor.
    4. Click Auto Import.
    5. Under Exclude from import and completion, click Add.
    6. Type org.jetbrains.annotations.NotNull
    7. Click Add again.
    8. Type org.jetbrains.annotations.Nullable
    9. Click OK.
  6. If you give your code to other people, their compiler won’t understand the annotations. What do you do to allow others to deal with your annotations? In general javac.exe just ignores annotations it does not recognise. Thus others will need your J:\Program Files (x86)\JetBrains\IntelliJ IDEA 10.5.2\redist\annotations.jar. You would have to bundle it with your source code distributions. They need to install annotations.jar in their JDK ext directories e.g.
    where to look for ext directories :
  7. Now insert some imports and annotations in your source code, and build.

Classpath Method

Mark Vedder said there is a much shorter way to accomplish this, that works by adding the annotations.jar to the classpath. I like to avoid using the classpath, since it so many cooks fiddle with it.
  1. Type the @Nullable or @NotNull annotations in your source code.
  2. Hit alt+enter to invoke quick fix and select Add 'annotations.jar' to classpath or Add Maven dependency if it’s a Maven project.
  3. Click OK to the dialog and continue working.
  4. I suspect you still have to do some of the above steps in the long method.

Puzzles

@Nullable How-To: notes on how to embed @Nullable and @NotNull in your code
annotation
empty
IntelliJ Idea

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/atnullable.html J:\mindprod\jgloss\atnullable.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.212]
You are visitor number 11.