{"record":{"id":"09d393857e7e084b","repo":"bilibili/DanmakuFlameMaster","slug":"input-stream-cannot-be-null","errorCode":null,"errorMessage":"input stream cannot be null!","messagePattern":"input stream cannot be null!","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/android/JSONSource.java","lineNumber":36,"sourceCode":"\r\n/**\r\n * a json file source\r\n * @author yrom\r\n */\r\npublic class JSONSource implements IDataSource<JSONArray>{\r\n\tprivate JSONArray mJSONArray;\r\n\tprivate InputStream mInput;\r\n\tpublic JSONSource(String json) throws JSONException{\r\n\t\tinit(json);\r\n\t}\r\n\t\r\n\tpublic JSONSource(InputStream in) throws JSONException{\r\n\t\tinit(in);\r\n\t}\r\n\t\r\n\tprivate void init(InputStream in) throws JSONException {\r\n\t\tif(in == null)\r\n\t\t\tthrow new NullPointerException(\"input stream cannot be null!\");\r\n\t\tmInput = in;\r\n\t\tString json = IOUtils.getString(mInput);\r\n\t\tinit(json);\r\n\t}\r\n\t\r\n\tpublic JSONSource(URL url) throws JSONException, IOException{\r\n\t\tthis(url.openStream());\r\n\t}\r\n\t\r\n\tpublic JSONSource(File file) throws FileNotFoundException, JSONException{\r\n\t\tinit(new FileInputStream(file));\r\n\t}\r\n\t\r\n\tpublic JSONSource(Uri uri) throws IOException, JSONException {\r\n\t\tString scheme = uri.getScheme();\r\n        if (SCHEME_HTTP_TAG.equalsIgnoreCase(scheme) || SCHEME_HTTPS_TAG.equalsIgnoreCase(scheme)) {\r\n            init(new URL(uri.getPath()).openStream());\r\n        } else if (SCHEME_FILE_TAG.equalsIgnoreCase(scheme)) {\r","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/bilibili/DanmakuFlameMaster/blob/e2846461a09e33720a049f628f09c653f55531f0/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/android/JSONSource.java#L18-L54","documentation":"JSONSource wraps a JSON input stream for the danmaku JSON parser; init(InputStream) validates the stream before reading and throws NullPointerException with a clear message when it is null, since IOUtils.getString would otherwise fail obscurely.","triggerScenarios":"new JSONSource((InputStream) null) — usually from calling openInputStream()/getAssets().open(...) and receiving null, or a variable assigned from a failed load being passed into the JSON danmaku loader.","commonSituations":"Asset file missing so Context.getAssets().open() throws or a wrapper returns null; ContentResolver.openInputStream returning null for a bad URI; network download that failed and its stream is null.","solutions":["Ensure the InputStream is valid before constructing: check in != null, or use Objects.requireNonNull(in).","Verify the asset/resource/URI exists and openInputStream() is not returning null.","If loading from a file or URL, prefer the JSONSource(File) or JSONSource(URL) constructors after confirming the source exists.","Wrap stream acquisition in try-catch and surface the underlying load failure instead of passing a null stream."],"exampleFix":"// before\nInputStream in = getAssets().open(\"comments.json\"); // may be null on failure\nJSONSource source = new JSONSource(in);\n// after\nInputStream in = getAssets().open(\"comments.json\");\nif (in != null) {\n    JSONSource source = new JSONSource(in);\n} else {\n    throw new FileNotFoundException(\"comments.json missing from assets\");\n}","handlingStrategy":"type-guard","validationCode":"InputStream in = resolver.openInputStream(uri);\nif (in == null) { throw new IOException(\"could not open stream for \" + uri); }\nJSONSource source = new JSONSource(in);","typeGuard":"boolean hasStream = (in != null);","tryCatchPattern":"try {\n    source = new JSONSource(in);\n} catch (NullPointerException e) {\n    Log.e(TAG, \"danmaku JSON input stream was null\", e);\n    source = null; // or retry with a fallback source\n}","preventionTips":["Check openInputStream()/getAssets().open() results for null before use.","Validate that the asset/file/URL exists before attempting to parse danmaku JSON.","Handle download failures so a null stream is never passed downstream."],"tags":["android","danmaku","json","null-argument","parser"],"backgroundTag":"null-argument","analyzedSha":"e2846461a09e33720a049f628f09c653f55531f0","analyzedAt":"2026-09-10T14:31:54.917Z","contentChangedAt":"2026-09-10T14:31:54.917Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}