bilibili/DanmakuFlameMaster · error · NullPointerException

input stream cannot be null!

Error message

input stream cannot be null!

What it means

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.

Solutions

  1. Ensure the InputStream is valid before constructing: check in != null, or use Objects.requireNonNull(in).
  2. Verify the asset/resource/URI exists and openInputStream() is not returning null.
  3. If loading from a file or URL, prefer the JSONSource(File) or JSONSource(URL) constructors after confirming the source exists.
  4. Wrap stream acquisition in try-catch and surface the underlying load failure instead of passing a null stream.

Example fix

// before
InputStream in = getAssets().open("comments.json"); // may be null on failure
JSONSource source = new JSONSource(in);
// after
InputStream in = getAssets().open("comments.json");
if (in != null) {
    JSONSource source = new JSONSource(in);
} else {
    throw new FileNotFoundException("comments.json missing from assets");
}
Defensive patterns

Strategy: type-guard

Validate before calling

InputStream in = resolver.openInputStream(uri);
if (in == null) { throw new IOException("could not open stream for " + uri); }
JSONSource source = new JSONSource(in);

Type guard

boolean hasStream = (in != null);

Try / catch

try {
    source = new JSONSource(in);
} catch (NullPointerException e) {
    Log.e(TAG, "danmaku JSON input stream was null", e);
    source = null; // or retry with a fallback source
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of bilibili/DanmakuFlameMaster@e2846461a0 (2026-09-10). Data as JSON: /api/errors/09d393857e7e084b. Report an issue: GitHub.

Appendix: source

Thrown at DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/android/JSONSource.java:36

/**
 * a json file source
 * @author yrom
 */
public class JSONSource implements IDataSource<JSONArray>{
	private JSONArray mJSONArray;
	private InputStream mInput;
	public JSONSource(String json) throws JSONException{
		init(json);
	}
	
	public JSONSource(InputStream in) throws JSONException{
		init(in);
	}
	
	private void init(InputStream in) throws JSONException {
		if(in == null)
			throw new NullPointerException("input stream cannot be null!");
		mInput = in;
		String json = IOUtils.getString(mInput);
		init(json);
	}
	
	public JSONSource(URL url) throws JSONException, IOException{
		this(url.openStream());
	}
	
	public JSONSource(File file) throws FileNotFoundException, JSONException{
		init(new FileInputStream(file));
	}
	
	public JSONSource(Uri uri) throws IOException, JSONException {
		String scheme = uri.getScheme();
        if (SCHEME_HTTP_TAG.equalsIgnoreCase(scheme) || SCHEME_HTTPS_TAG.equalsIgnoreCase(scheme)) {
            init(new URL(uri.getPath()).openStream());
        } else if (SCHEME_FILE_TAG.equalsIgnoreCase(scheme)) {

View on GitHub (pinned to e2846461a0)