This error occurs when the class under test can throw a
NullPointerException when one of its fields is null and
toString is called. For example,
equals could contain this line:
It will throw a
foo is null. This can be avoided in three ways:
- In Java 7 and up, you can use
Objects.equals, which is null-safe:
- You can add a null check, like so:
If you’re certain the field can never be null (for instance, because the class’s constructor explicitly checks for it), you can add
.withNonnullFields("foo")to your call to EqualsVerifier
If this problem occurs for many fields in the same class, you can suppress
Warning.NULL_FIELDSin your call to EqualsVerifier.
You can read more about dealing with null in the manual page about null.