{"record":{"id":"cf34207f89081163","repo":"didi/DoKit","slug":"key-null-bitmap-null","errorCode":null,"errorMessage":"key == null || bitmap == null","messagePattern":"key == null \\|\\| bitmap == null","errorType":"validation","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/LruCache.java","lineNumber":69,"sourceCode":"      throw new NullPointerException(\"key == null\");\n    }\n\n    Bitmap mapValue;\n    synchronized (this) {\n      mapValue = map.get(key);\n      if (mapValue != null) {\n        hitCount++;\n        return mapValue;\n      }\n      missCount++;\n    }\n\n    return null;\n  }\n\n  @Override public void set(String key, Bitmap bitmap) {\n    if (key == null || bitmap == null) {\n      throw new NullPointerException(\"key == null || bitmap == null\");\n    }\n\n    Bitmap previous;\n    synchronized (this) {\n      putCount++;\n      size += Utils.getBitmapBytes(bitmap);\n      previous = map.put(key, bitmap);\n      if (previous != null) {\n        size -= Utils.getBitmapBytes(previous);\n      }\n    }\n\n    trimToSize(maxSize);\n  }\n\n  private void trimToSize(int maxSize) {\n    while (true) {\n      String key;","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/LruCache.java#L51-L87","documentation":"LruCache.set() throws NullPointerException when either the key or the bitmap is null. Storing a null bitmap would corrupt accounting (byte sizes) and a null key could never be retrieved, so both are rejected. Fail-fast guard.","triggerScenarios":"Calling cache.set(key, null) after a decode returned null, or cache.set(null, bitmap) when the cache key is missing.","commonSituations":"Caching a decode result without checking decode success; cache key derived from a URI that is null for resource-ID requests.","solutions":["Only call set() when both key and bitmap are non-null","Check the decode result: skip caching when the factory returned null","Guarantee key generation (stableKey/URI/resourceId) before any cache interaction"],"exampleFix":"// before\nBitmap b = decode(data);\ncache.set(key, b); // NullPointerException when b == null\n\n// after\nBitmap b = decode(data);\nif (b != null && key != null) {\n  cache.set(key, b);\n}","handlingStrategy":"validation","validationCode":"if (key != null && bitmap != null) { cache.set(key, bitmap); }","typeGuard":"boolean canCache(String key, Bitmap bitmap) { return key != null && bitmap != null; }","tryCatchPattern":null,"preventionTips":["Skip caching when a decode returns null instead of storing it","Generate the cache key before starting the decode so it is always available"],"tags":["android","picasso","cache","null-check"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}