This project demonstrates how to integrate NullAway into a Bazel Java project using Error Prone.
NullAway is a tool to help eliminate NullPointerExceptions (NPEs) in your Java code with low build-time overhead. This example shows a pattern for automatically applying NullAway to all Java targets in your project using custom Bazel rule wrappers.
bazel/rules/java.bzl: Contains custom wrappers forjava_library,java_binary, andjava_test. These wrappers automatically add the NullAway Java plugin and the necessaryjavacoptsto enable null checking.bazel/rules/BUILD.bazel: Defines thejava_pluginfor NullAway and Error Prone.src/main/java/com/example/: A sample Java application using JSpecify annotations (@Nullable).
- Bazelisk (to manage the Bazel version specified in
.bazelversion) - Java JDK 25
To build the project:
bazel build //...To run the tests:
bazel test //...To see NullAway in action, try modifying src/main/java/com/example/Hello.java to dereference a nullable string without a null check:
public static void main(String[] args) {
String s = getNullableString();
// This should trigger a NullAway error
System.out.println(s.length());
}When you run bazel build //..., the build should fail with an error from NullAway.
NullAway is configured in bazel/rules/java.bzl with the following options:
-Xep:NullAway:ERROR: Treats NullAway warnings as errors.-XepOpt:NullAway:AnnotatedPackages=com.example: Specifies which packages NullAway should analyze.-XDcompilePolicy=simple: Required for Error Prone plugins.
This project is released under the Unlicense. See LICENSE for details.