google/guava · error · NullPointerException
at index %s
Error message
at index %s
What it means
EqualsTester.addEqualityGroup(...) forbids null elements. EqualsTester always verifies on its own that every object returns false from equals(null), so letting the user also pass null would be redundant and ambiguous about which group it belongs to. A null in the varargs array triggers a NullPointerException naming the offending index.
Source
Thrown at android/guava-testlib/src/com/google/common/testing/EqualsTester.java:117
*
* <p>The {@code @Nullable} annotations on the {@code equalityGroup} parameter imply that the
* objects, and the array itself, can be null. That is for programmer convenience, when the
* objects come from factory methods that are themselves {@code @Nullable}. In reality neither the
* array nor its contents can be null, but it is not useful to force the use of {@code
* requireNonNull} or the like just to assert that.
*
* <p>{@code EqualsTester} will always check that every object it is given returns false from
* {@code equals(null)}, so it is neither useful nor allowed to include a null value in any
* equality group.
*/
@CanIgnoreReturnValue
public EqualsTester addEqualityGroup(@Nullable Object @Nullable ... equalityGroup) {
checkNotNull(equalityGroup);
List<Object> list = new ArrayList<>(equalityGroup.length);
for (int i = 0; i < equalityGroup.length; i++) {
Object element = equalityGroup[i];
if (element == null) {
throw new NullPointerException("at index " + i);
}
list.add(element);
}
equalityGroups.add(list);
return this;
}
/** Run tests on equals method, throwing a failure on an invalid test */
@CanIgnoreReturnValue
public EqualsTester testEquals() {
RelationshipTester<Object> delegate =
new RelationshipTester<>(
Equivalence.equals(), "Object#equals", "Object#hashCode", itemReporter);
for (List<Object> group : equalityGroups) {
delegate.addRelatedGroup(group);
}
for (int run = 0; run < REPETITIONS; run++) {
testItems();View on GitHub (pinned to 94f39958ba)
Solutions
- Remove the null element from the group; EqualsTester already asserts equals(null) == false for every object.
- If you meant to test a null-like case, substitute Optional.empty(), a sentinel object, or an empty collection — never null.
- Filter nulls out before constructing the group when sourcing from a nullable collection.
Example fix
// before tester.addEqualityGroup(a, null, b); // NullPointerException: at index 1 // after tester.addEqualityGroup(a, b); // equals(null) checked automatically
Defensive patterns
Strategy: validation
Validate before calling
// Strip nulls before forming a group; EqualsTester checks equals(null) itself.
Object[] safe = Arrays.stream(maybeWithNulls)
.filter(Objects::nonNull)
.toArray();
new EqualsTester().addEqualityGroup(safe).testEquals(); Type guard
// If you build groups dynamically, guard each element.
static boolean isGroupable(Object o) { return o != null; } Prevention
- Never pass null in addEqualityGroup(...); EqualsTester already tests equals(null) == false.
- When sourcing a group from a collection, filter nulls first.
- Represent 'absent' with Optional.empty() or a sentinel object, not null.
- Treat the NullPointerException: at index N message as a data error in your test inputs.
When it happens
Trigger: Calling new EqualsTester().addEqualityGroup(a, null, b) — i.e. passing a null reference anywhere in the equalityGroup varargs. The check runs on every element before the group is stored.
Common situations: Converting a List/Map that contains null values into an array passed to addEqualityGroup; misunderstanding the API and trying to test equals(null) yourself; arrays widened to Object[] that sneak in a null.
Related errors
- $ITEM must not be $RELATIONSHIP to $OTHER
- %s returns null and cannot be used to test instance methods.
- wrong exception thrown from %s when passing null to %s param
- Unexpected interrupt while waiting for future
- Unexpected interrupt while waiting for latch
AI-assisted analysis of google/guava@94f39958ba (2026-08-13).
Data as JSON: /api/errors/9bc724a41c3fe44c.
Report an issue: GitHub.