Tencent/matrix · error · NullPointerException

must not be null

Error message

${name} must not be null

What it means

Preconditions.checkNotNull is a Guava-style helper that throws NullPointerException('<name> must not be null') when the given instance is null. It is used across matrix-resource-canary-common to validate constructor and method arguments early. The message names the parameter so the caller can identify which required value was missing.

Solutions

  1. Ensure the argument is non-null before calling; provide a default instance where the API allows.
  2. Fix initialization order so the dependency exists before the canary component is constructed.
  3. If the parameter is truly optional, use the API variant that accepts null or supply a no-op implementation.
  4. Log/inspect the message's name field to identify exactly which argument was null.

Example fix

// before
watcherFactory.setLeakProcessor(null);
// after
watcherFactory.setLeakProcessor(new DefaultLeakProcessor(new DefaultHeapDump()));
Defensive patterns

Strategy: validation

Validate before calling

if (arg == null) {
    throw new IllegalArgumentException("arg must be provided before calling API");
}

Type guard

static <T> boolean isPresent(T v) { return v != null; }

Try / catch

try {
    api.call(maybeNull);
} catch (NullPointerException e) {
    Log.e(TAG, "Missing required argument: " + e.getMessage(), e);
}

Prevention

When it happens

Trigger: Passing null as any argument wrapped in Preconditions.checkNotNull(instance, "name") — e.g. null activity, null listener, null file path, or null config object supplied to ResourcePlugin/WatcherFactory APIs.

Common situations: Builder fields left unset before build(); initialization ordering where a dependency (e.g. application context, plugin listener) isn't ready; passing null intentionally to 'disable' a feature where the API demands a real object.

Related errors


AI-assisted analysis of Tencent/matrix@3b8293bd65 (2026-09-08). Data as JSON: /api/errors/a9ef93a9531e4af8. Report an issue: GitHub.

Appendix: source

Thrown at matrix/matrix-android/matrix-resource-canary/matrix-resource-canary-common/src/main/java/com/tencent/matrix/resource/common/utils/Preconditions.java:32

 * limitations under the License.
 */
package com.tencent.matrix.resource.common.utils;

/**
 * Created by tangyinsheng on 2017/6/2.
 *
 * This class is ported from LeakCanary.
 */
public final class Preconditions {

    /**
     * Returns instance unless it's null.
     *
     * @throws NullPointerException if instance is null
     */
    public static <T> T checkNotNull(T instance, String name) {
        if (instance == null) {
            throw new NullPointerException(name + " must not be null");
        }
        return instance;
    }

    private Preconditions() {
        throw new AssertionError();
    }
}

View on GitHub (pinned to 3b8293bd65)