{"record":{"id":"d8cfe28dcd021aeb","repo":"google/guava","slug":"null-check-failed-on-return-value-of-s","errorCode":null,"errorMessage":"Null check failed on return value of %s","messagePattern":"Null check failed on return value of (.+?)","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java","lineNumber":468,"sourceCode":"\n    /**\n     * Tests null checks against the instance methods of the return values, if any.\n     *\n     * <p>Test fails if default value cannot be determined for a constructor or factory method\n     * parameter, or if the constructor or factory method throws exception.\n     *\n     * @return this tester\n     */\n    @CanIgnoreReturnValue\n    public FactoryMethodReturnValueTester testNulls() throws Exception {\n      for (Invokable<?, ?> factory : getFactoriesToTest()) {\n        Object instance = instantiate(factory);\n        if (instance != null\n            && packagesToTest.contains(Reflection.getPackageName(instance.getClass()))) {\n          try {\n            nullPointerTester.testAllPublicInstanceMethods(instance);\n          } catch (AssertionError e) {\n            throw new AssertionError(\"Null check failed on return value of \" + factory, e);\n          }\n        }\n      }\n      return this;\n    }\n\n    /**\n     * Tests {@link Object#equals} and {@link Object#hashCode} against the return values of the\n     * static methods, by asserting that when equal parameters are passed to the same static method,\n     * the return value should also be equal; and vice versa.\n     *\n     * <p>Test fails if default value cannot be determined for a constructor or factory method\n     * parameter, or if the constructor or factory method throws exception.\n     *\n     * @return this tester\n     */\n    @CanIgnoreReturnValue\n    public FactoryMethodReturnValueTester testEquals() throws Exception {","sourceCodeStart":450,"sourceCodeEnd":486,"githubUrl":"https://github.com/google/guava/blob/94f39958baf7ad51ddf9c70e406ed6b188194daa/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java#L450-L486","documentation":"Thrown by ClassSanityTester.FactoryMethodReturnValueTester.testNulls() when nullPointerTester.testAllPublicInstanceMethods(instance) fails on an object returned by a static factory. The wrapping AssertionError records which factory produced the bad instance; the cause (AssertionError) describes which method failed the null-check.","triggerScenarios":"Calling ClassSanityTester.setDefault(...)/test(...)/testNulls() on a class; one of its static factory methods returns an instance whose public methods do not reject null parameters (missing @Nullable annotation + missing check).","commonSituations":"Adding a new public method that accepts parameters without null checks or @Nullable marks; auto-generating code; returning a wrapper/delegate that forwards nulls unsafely.","solutions":["Read the cause to find the failing method and parameter; add a null check (checkNotNull/requireNonNull) or annotate the parameter @Nullable if null is intentionally accepted.","Run testNulls() in isolation on the factory to iterate quickly.","Ensure every public instance method either rejects nulls explicitly or declares @Nullable on accepted-null params."],"exampleFix":"// before\npublic Foo withName(String name) {\n  this.name = name.toUpperCase(); // NPE later, not at boundary\n  return this;\n}\n\n// after\npublic Foo withName(String name) {\n  this.name = checkNotNull(name).toUpperCase();\n  return this;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  factoryTester.testNulls();\n} catch (AssertionError e) {\n  // e.getCause() is the null-pointer AssertionError naming the failing method/param\n  fail(\"Factory \" + factory + \" produced an instance failing null checks: \" + e.getCause());\n}","preventionTips":["Add explicit null checks (checkNotNull/requireNonNull) or @Nullable at every public method parameter boundary.","Run ClassSanityTester.testNulls() as part of the build for every public value type."],"tags":["guava-testlib","null-checks","reflection","class-sanity"],"backgroundTag":null,"analyzedSha":"94f39958baf7ad51ddf9c70e406ed6b188194daa","analyzedAt":"2026-08-13T22:50:30.265Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}