asLody/VirtualApp · error · java.lang.IllegalStateException

Invalid userId

Error message

Invalid userId 

What it means

VirtualStorageService manages per-user virtual storage paths inside VirtualApp. checkUserId validates that the given userId corresponds to an existing virtual user (per VUserManagerService.exists). If the user does not exist, it throws IllegalStateException("Invalid userId ..."), and this guard runs at the start of setVirtualStorage, getVirtualStorage, setVirtualStorageState, and isVirtualStorageEnable.

Solutions

  1. Create the virtual user first (VUserManagerService.createUser) before touching its virtual storage
  2. Validate the userId with VUserManagerService.get().exists(userId) before the call and fail fast with your own message
  3. Ensure the userId passed around your app is the same convention VA uses (int index as returned by user creation/listing APIs)
  4. Catch the IllegalStateException and re-initialize user + storage if the user is missing

Example fix

// before
storage.setVirtualStorage(userId, path);
// after
if (!VUserManagerService.get().exists(userId)) {
    throw new IllegalArgumentException("user " + userId + " must be created first");
}
storage.setVirtualStorage(userId, path);
Defensive patterns

Strategy: validation

Validate before calling

// Java: guard before any VirtualStorageService call
VUserManagerService ums = VUserManagerService.get();
if (ums == null || !ums.exists(userId)) {
    throw new IllegalArgumentException("virtual user " + userId + " does not exist");
}

Type guard

boolean validUser(int userId) {
    VUserManagerService ums = VUserManagerService.get();
    return ums != null && userId >= 0 && ums.exists(userId);
}

Prevention

When it happens

Trigger: Calling any of those four APIs with a userId that was never created via VUserManagerService (e.g. userId 0 assumptions that don't hold, a user that was removed, or a hardcoded/hard-typed ID from config).

Common situations: Calling virtual-storage APIs before any virtual users have been created during first-run initialization; using a userId saved before the user was deleted; off-by-one or 1-based userId values passed where 0-based were expected.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of asLody/VirtualApp@666fefcb5d (2026-09-09). Data as JSON: /api/errors/5b90513596a094bb. Report an issue: GitHub.

Appendix: source

Thrown at VirtualApp/lib/src/main/java/com/lody/virtual/server/vs/VirtualStorageService.java:91

            config.enable = enable;
            mLayer.save();
        }

    }

    @Override
    public boolean isVirtualStorageEnable(String packageName, int userId) {
        checkUserId(userId);
        synchronized (mConfigs) {
            VSConfig config = getOrCreateVSConfigLocked(packageName, userId);
            return config.enable;
        }

    }

    private void checkUserId(int userId) {
        if (!VUserManagerService.get().exists(userId)) {
            throw new IllegalStateException("Invalid userId " + userId);
        }
    }
}

View on GitHub (pinned to 666fefcb5d)