Geertjan's Blog

  • December 12, 2012

NetBeans IDE 7.3 Knows Null

Geertjan Wielenga
Product Manager

What's the difference between these two methods, "test1" and "test2"?

public int test1(String str) {
    return str.length();

public int test2(String str) {
    if (str == null) {
        System.err.println("Passed null!.");
        //forgotten return;
    return str.length();

The difference, or at least, the difference that is relevant for this blog entry, is that whoever wrote "test2" apparently thinks that the variable "str" may be null, though did not provide a null check. In NetBeans IDE 7.3, you see this hint for "test2", but no hint for "test1", since in that case we don't know anything about the developer's intention for the variable and providing a hint in that case would flood the source code with too many false positives:

 Annotations are supported in understanding how a piece of code is intended to be used. If method return types use @Nullable, @NullAllowed, @CheckForNull, the value is considered to be "strongly possible to be null", as well as if the variable is tested to be null, as shown above. When using @NotNull, @NonNull, @Nonnull, the value is considered to be non-null. (The exact FQNs of the annotations are ignored, only simple names are checked.)

Here are examples showing where the hints are displayed for the non-null hints (the "strongly possible to be null" hints are not shown below, though you can see one of them in the screenshot above), together with a comment showing what is shown when you hover over the hint:

There isn't a "one size fits all" refactoring for these various instances relating to null checks, hence you can't do an automated refactoring across your code base via tools in NetBeans IDE, as shown yesterday for class member reordering across code bases. However, you can, instead, go to Source | Inspect and then do a scan throughout a scope (e.g., current file/package/project or combinations of these or all open projects) for class elements that the IDE identifies as potentially having a problem in this area:

Thanks to Jan Lahoda, who reports that this currently also works in NetBeans IDE 7.3 dev builds for fields but that may need to be disabled since right now too many false positives are returned, for help with the info above and any misunderstandings are my own fault!

Join the discussion

Comments ( 5 )
  • Michael Kroll Thursday, December 13, 2012

    Great, I thing 7.3 is a big step forward.

  • Tex Friday, December 14, 2012

    Great !

    Wonderful hint.

    I'm using Netbeans since 2000 and I love it, this is absolutely the best IDE !!!

  • Werner Keil Monday, December 17, 2012

    Is this related to either JSR 305 or 308?

  • Geertjan Tuesday, December 18, 2012

    The JSR305 meta annotations are not supported at this time.

  • Infernoz Thursday, December 20, 2012

    Nice feature, but already supported by existing code checker plug-in functionality.

    These code hints often depend on cached class meta data, which is fine if you make small changes to projects; however you don't even see these hints

    if a lengthy meta data rebuild is in progress.

    I'd much rather know that serious work is concentrated in 7.3 on ending the ridiculous and performance crippling use of huge numbers of files in the cache directory, and stop prematurely merging meta data, because this can cause massive performance and rebuild delays for large multi-module (especially Maven) projects in Filesystems like NTFS, even on fast flash disks. Proper databases do not idiotically treat a filesystem as a database by splitting data across a huge numbers of files keyed by filename; they use optimised indexes, clobs, blobs and relational data merged into far fewer files, because it uses much less costly IO resources and is vastly faster!

    I'd also like to see the ability to manually mark a whole project as dirty, to force a meta data rebuild when NetBeans does not detect inconsistencies, so that I don't have to delete the whole cache directory, which impacts multiple other projects!

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.