LSPosed/LSPosed · error · IllegalArgumentException

listener cannot be null

Error message

listener cannot be null

What it means

XSharedPreferences.registerOnSharedPreferenceChangeListener(listener) throws IllegalArgumentException('listener cannot be null') for a null listener. The registration stores the listener as a key in the mListeners map used to drive the file-watcher notifications, so a null key is unrepresentable — this mirrors the framework SharedPreferences contract.

Source

Thrown at core/src/main/java/de/robv/android/xposed/XSharedPreferences.java:526

     */
    @Deprecated
    @Override
    public Editor edit() {
        throw new UnsupportedOperationException("read-only implementation");
    }

    /**
     * Registers a callback to be invoked when a change happens to a preference file.<br>
     * Note that it is not possible to determine which preference changed exactly and thus
     * preference key in callback invocation will always be null.
     *
     * @param listener The callback that will run.
     * @see #unregisterOnSharedPreferenceChangeListener
     */
    @Override
    public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
        if (listener == null)
            throw new IllegalArgumentException("listener cannot be null");

        synchronized (this) {
            if (mListeners.put(listener, sContent) == null) {
                tryRegisterWatcher();
            }
        }
    }

    /**
     * Unregisters a previous callback.
     *
     * @param listener The callback that should be unregistered.
     * @see #registerOnSharedPreferenceChangeListener
     */
    @Override
    public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
        synchronized (this) {
            if (mListeners.remove(listener) != null && mListeners.isEmpty()) {

View on GitHub (pinned to df74d83eb0)

Solutions

  1. Null-check before registering: if (listener != null) prefs.registerOnSharedPreferenceChangeListener(listener);
  2. Ensure the listener instance is created before the registration statement executes.
  3. Unregister the same non-null instance on teardown to avoid watcher leaks.

Example fix

// before
prefs.registerOnSharedPreferenceChangeListener(listener); // listener == null -> throws
// after
if (listener != null) prefs.registerOnSharedPreferenceChangeListener(listener);
Defensive patterns

Strategy: validation

Validate before calling

if (listener != null) prefs.registerOnSharedPreferenceChangeListener(listener);

Prevention

When it happens

Trigger: Passing a listener variable that was never assigned, or a listener field nulled before a register/unregister pair, e.g. registerListener(field) where field is conditionally initialized elsewhere.

Common situations: Refactor leaves the listener assignment in a code path not taken; DI/testing passes null; a race where the listener is detached and set null then re-registered.

Related errors


AI-assisted analysis of LSPosed/LSPosed@df74d83eb0 (2026-08-14). Data as JSON: /api/errors/a63f7f429d9fe13c. Report an issue: GitHub.