{"record":{"id":"8ce25fc2284de9d8","repo":"TeamNewPipe/NewPipe","slug":"cache-dir-does-not-exist","errorCode":null,"errorMessage":"Cache dir does not exist > ","messagePattern":"Cache dir does not exist > ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/schabi/newpipe/util/StateSaver.java","lineNumber":228,"sourceCode":"        final LinkedList<Object> savedObjects = new LinkedList<>();\n        writeRead.writeTo(savedObjects);\n\n        if (isChangingConfig) {\n            if (savedObjects.size() > 0) {\n                STATE_OBJECTS_HOLDER.put(prefixFileName, savedObjects);\n                return new SavedState(prefixFileName, \"\");\n            } else {\n                if (MainActivity.DEBUG) {\n                    Log.d(TAG, \"Nothing to save\");\n                }\n                return null;\n            }\n        }\n\n        try {\n            File cacheDir = new File(cacheDirPath);\n            if (!cacheDir.exists()) {\n                throw new RuntimeException(\"Cache dir does not exist > \" + cacheDirPath);\n            }\n            cacheDir = new File(cacheDir, CACHE_DIR_NAME);\n            if (!cacheDir.exists()) {\n                if (!cacheDir.mkdir()) {\n                    if (BuildConfig.DEBUG) {\n                        Log.e(TAG,\n                                \"Failed to create cache directory \" + cacheDir.getAbsolutePath());\n                    }\n                    return null;\n                }\n            }\n\n            final File file = new File(cacheDir, prefixFileName\n                    + (TextUtils.isEmpty(suffixFileName) ? \".cache\" : suffixFileName));\n            if (file.exists() && file.length() > 0) {\n                // If the file already exists, just return it\n                return new SavedState(prefixFileName, file.getAbsolutePath());\n            } else {","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/TeamNewPipe/NewPipe/blob/9e8be091560a69f35d44bd252eb00bc7911c977b/app/src/main/java/org/schabi/newpipe/util/StateSaver.java#L210-L246","documentation":"Thrown by StateSaver when the cache directory path (cacheDirPath) passed to the save routine does not exist as a file/directory on disk. StateSaver persists download/playback state to a cache directory; it first checks cacheDir.exists() and throws a RuntimeException if false. This is a hard precondition: the parent cache path must already exist before state can be saved into a subdirectory beneath it.","triggerScenarios":"StateSaver.saveState or the underlying method receives a cacheDirPath whose File does not exist(). The check at line 226 fails. Occurs when the external cache dir (getExternalFilesDir result) was on a just-unmounted SD card, when the app's cache was cleared by the OS under storage pressure, or when an invalid/nonexistent path was configured.","commonSituations":"Removable storage unmounted between sessions; the OS purged the app's cache directory; a path pointing to external storage that isn't mounted on this device; running on a device where getExternalFilesDir returned a path that was then removed.","solutions":["Use context.getCacheDir() (internal, always available) as the cache path rather than external storage.","Recreate the cache directory (mkdirs) before passing the path to StateSaver if it may have been removed.","Catch the RuntimeException and skip state-saving gracefully (state is cache; loss is non-fatal).","Validate cacheDir.exists() and fall back to an alternate location at app startup."],"exampleFix":"// before\nFile cacheDir = new File(cacheDirPath);\nif (!cacheDir.exists()) {\n    throw new RuntimeException(\"Cache dir does not exist > \" + cacheDirPath);\n}\n\n// after — recreate or fall back\nFile cacheDir = new File(cacheDirPath);\nif (!cacheDir.exists() && !cacheDir.mkdirs()) {\n    cacheDir = context.getCacheDir(); // internal fallback\n}\nif (!cacheDir.exists()) {\n    return null; // state-saving is best-effort\n}","handlingStrategy":"try-catch","validationCode":"// Ensure the cache directory exists before StateSaver uses it:\nFile cacheDir = new File(cacheDirPath);\nif (!cacheDir.exists() && !cacheDir.mkdirs()) {\n    cacheDirPath = context.getCacheDir().getAbsolutePath(); // internal fallback\n}","typeGuard":null,"tryCatchPattern":"try {\n    SavedState state = StateSaver.saveState(...);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Cache dir does not exist\")) {\n        // cache is gone — state saving is best-effort, skip silently\n        Log.w(TAG, \"Cache dir missing, skipping state save\", e);\n    } else throw e;\n}","preventionTips":["Prefer context.getCacheDir() (internal, always available) for state files.","Create the cache directory with mkdirs() at app startup.","Treat StateSaver failures as non-fatal since it is cache-only data."],"tags":["state-management","cache","storage","io","android"],"backgroundTag":null,"analyzedSha":"9e8be091560a69f35d44bd252eb00bc7911c977b","analyzedAt":"2026-08-14T00:11:33.519Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}