google/guava · error · AssertionError

wrong exception thrown from %s when passing null to %s param

Error message

wrong exception thrown from %s when passing null to %s parameter at index %s.%nFull parameters: %s%nActual exception message: %s

What it means

NullPointerTester passes null to one parameter at a time (others default) and expects either NullPointerException or another exception type you have explicitly registered as acceptable. If the code under test throws a different type (e.g. IllegalArgumentException, ClassCastException, a custom RuntimeException), the test fails with this AssertionError because the null precondition is not signaled correctly.

Source

Thrown at android/guava-testlib/src/com/google/common/testing/NullPointerTester.java:418

    @Nullable Object[] params = buildParamList(invokable, paramIndex);
    try {
      @SuppressWarnings("unchecked") // We'll get a runtime exception if the type is wrong.
      Invokable<Object, ?> unsafe = (Invokable<Object, ?>) invokable;
      unsafe.invoke(instance, params);
      Assert.fail(
          "No exception thrown for parameter at index "
              + paramIndex
              + " from "
              + invokable
              + Arrays.toString(params)
              + " for "
              + testedClass);
    } catch (InvocationTargetException e) {
      Throwable cause = e.getCause();
      if (policy.isExpectedType(cause)) {
        return;
      }
      throw new AssertionError(
          String.format(
              "wrong exception thrown from %s when passing null to %s parameter at index %s.%n"
                  + "Full parameters: %s%n"
                  + "Actual exception message: %s",
              invokable,
              invokable.getParameters().get(paramIndex).getType(),
              paramIndex,
              Arrays.toString(params),
              cause),
          cause);
    } catch (IllegalAccessException e) {
      throw new RuntimeException(e);
    }
  }

  private @Nullable Object[] buildParamList(
      Invokable<?, ?> invokable, int indexOfParamToSetToNull) {
    ImmutableList<Parameter> params = invokable.getParameters();

View on GitHub (pinned to 94f39958ba)

Solutions

  1. Add an explicit null check that throws NullPointerException: Objects.requireNonNull(param) or checkNotNull(param) at the top of the method.
  2. If a non-NPE type is genuinely correct for that parameter, annotate the parameter @Nullable so NullPointerTester skips it.
  3. Register the accepted exception type with tester.setDefault(Enum.class,...)/NullPointerTester policy if the API supports it, or use the per-method overload to whitelist the expected type.
  4. Restructure the guard so checkNotNull runs before checkArgument/checkState.

Example fix

// before
public Foo create(String name, int mode) {
  checkArgument(mode >= 0, "mode"); // null name -> NPE later, wrong param
  this.name = name.toLowerCase();
}

// after
public Foo create(String name, int mode) {
  this.name = checkNotNull(name).toLowerCase();
  checkArgument(mode >= 0, "mode");
}
Defensive patterns

Strategy: validation

Validate before calling

// Make null-safety explicit so NullPointester sees NPE, not a wrong type.
// Verify before running the tester that each non-@Nullable parameter is guarded:
public Foo create(String name, int mode) {
  this.name = Objects.requireNonNull(name, "name");
  if (mode < 0) throw new IllegalArgumentException("mode");
  ...
}

Type guard

// Mark parameters that legitimately accept null so the tester skips them.
public Bar compute(@Nullable String hint, int x) { ... }

Try / catch

// AssertionError from NullPointerTester is a test failure; don't swallow it.
// Fix the production null guard (preferred) or annotate @Nullable (skip).

Prevention

When it happens

Trigger: NullPointerTester.testEquals/testAllPublicMethods/testAllPublicConstructors on a type whose method, when a given parameter is null, throws something other than NullPointerException — e.g. Preconditions.checkArgument(cond) without a prior null check, or a field access that surfaces as a different exception.

Common situations: Methods guarded only by checkArgument/checkState (throw IllegalArgumentException), auto-unboxing of a null Integer, delegation to a library that throws a non-NPE, or custom validation exceptions used for null arguments.

Related errors


AI-assisted analysis of google/guava@94f39958ba (2026-08-13). Data as JSON: /api/errors/580a63ac0f7192b6. Report an issue: GitHub.