1

I am setting up my Intellij environment and I am wondering how to turn on Nullable warnings for the following:

Map<String, String> simpleMap = new HashMap<>();
String thisWillBeNull = simpleMap.get("key");
int l = thisWillBeNull.length();  //<-- how do I get a Nullable warning here?
Mfswiggs
  • 307
  • 1
  • 6
  • 16
  • "how do I get a NullPointerException here?" Why do you think you can get NPE there? `this` is never null. Do you mean `thisWillBeNull.length()`? (But in general, I doubt you can really detect that `thisWillBeNull` *will* be null, as opposed to merely Nullable). – Andy Turner Aug 09 '19 at 13:16
  • 1
    That's probably too complex to detect by the IDE. It doesn't know the state of the Map. If you did `String thisWillBeNull = null;` instead, the IDE should be able to detect it. – Roger Gustavsson Aug 09 '19 at 13:29
  • @RogerGustavsson it shouldn't be too hard, the Map [documentation](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#get-java.lang.Object-) states that `get(...)` can return `null`. – Mfswiggs Aug 09 '19 at 13:44
  • "Can" doesn't mean that it always will do. That's a big difference. – Roger Gustavsson Aug 09 '19 at 13:52
  • @RogerGustavsson right, but I just want Intellij to tell me that it "can" be null. – Mfswiggs Aug 09 '19 at 14:01
  • I don't see the point. Every variable in your code "can" be null. It's only interesting to be told by the IDE when a variable will always be null. – Roger Gustavsson Aug 09 '19 at 14:12
  • A variable won't be null if it comes from a nonnull method. Additionally it is good to know when a variable can be null so that null checks can be performed. – Mfswiggs Aug 09 '19 at 14:24
  • Indeed, this particular situation could be detected. Try kotlin ;-) – Laurent G Aug 09 '19 at 15:32

4 Answers4

1

IntelliJ's nullity analysis is useful, but limited. It has weak handling for maps.

If you wish to obtain guarantees about code that uses maps, you could use the Nullness Checker, which includes a map key analysis. It isn't built into IntelliJ, but you can integrate it with IntelliJ or run it as a separate tool.

Given this code:

import java.util.HashMap;
import java.util.Map;

public class MapGetTest {
  void m() {
    Map<String, String> simpleMap = new HashMap<>();
    String thisWillBeNull = simpleMap.get("key");
    int l = thisWillBeNull.length();
  }
}

You can run this command:

javac -processor nullness MapGetTest.java

and the result is:

MapGetTest.java:8: error: [dereference.of.nullable] dereference of possibly-null reference thisWillBeNull
    int l = thisWillBeNull.length();
            ^
1 error
mernst
  • 7,437
  • 30
  • 45
  • I do like this solution! The only problem I am getting now is that if `simpleMap` is populated somewhere else then I still get this error... – Mfswiggs Aug 15 '19 at 11:52
  • Can you post a [MWE](https://stackoverflow.com/help/minimal-reproducible-example) (probably in a different question)? – mernst Aug 15 '19 at 15:04
0

There is an option in Intellij:

Go to Analyze -> Inspect Code

This will analyze the code and point out possible issues in the code.

halfer
  • 19,824
  • 17
  • 99
  • 186
MRTJ
  • 141
  • 7
0

Generally, two things are needed to give tools a chance to detect the problem:

  • analysis must use null annotations to enable inter-procedural analysis
  • method Map.get() must be annotated to return @Nullable V. For library classes this may happen using external annotations.
Stephan Herrmann
  • 7,963
  • 2
  • 27
  • 38
0

I had the same question, so I asked a similar question in Intellij community

Here is the answer: https://youtrack.jetbrains.com/issue/IDEA-289285#focus=Comments-27-5802422.0-0

Solution

  1. add @Nullable to java.util.Map interface
  2. create a new Structural Search Inspection. However, it seems that you cannot share this config with your team by git directly.

enter image description here

Jess Chen
  • 3,136
  • 1
  • 26
  • 35