Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

NullPointerExceptions parsing non-strict JSON using JSONTokener from version 20241224 #933

Closed
bherw opened this issue Jan 8, 2025 · 2 comments · Fixed by #937 or #938
Closed

NullPointerExceptions parsing non-strict JSON using JSONTokener from version 20241224 #933

bherw opened this issue Jan 8, 2025 · 2 comments · Fixed by #937 or #938

Comments

@bherw
Copy link

bherw commented Jan 8, 2025

String s = "{\"a\": [],}";
var tokener = new JSONTokener(s);
var value = tokener.nextValue();
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.json.JSONParserConfiguration.isStrictMode()" because "jsonParserConfiguration" is null
	at org.json.JSONObject.<init>(JSONObject.java:280)
	at org.json.JSONTokener.nextValue(JSONTokener.java:429)
	at Scratch.main(scratch_3.java:8)
String s = "[1,]";
var tokener = new JSONTokener(s);
var value = tokener.nextValue();
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.json.JSONParserConfiguration.isStrictMode()" because "jsonParserConfiguration" is null
	at org.json.JSONArray.<init>(JSONArray.java:144)
	at org.json.JSONTokener.nextValue(JSONTokener.java:436)
	at Scratch.main(scratch_3.java:8)

It looks like the parsing implementations in the JSONObject and JSONArray constructors that take a JSONTokener don't check if the configuration is null when encountering non-standard JSON input.

@Simulant87
Copy link
Contributor

Simulant87 commented Jan 8, 2025

reproduced with test case in #936 and providing a fix with null checks.

@stleary
Copy link
Owner

stleary commented Jan 9, 2025

@bherw Thanks for catching this.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
3 participants