Skip to content

Treat null name arrays as empty JSONObject selections - #1097

Open
illusionoftalha wants to merge 1 commit into
stleary:masterfrom
illusionoftalha:fix/null-subset-constructor-names
Open

illusionoftalha wants to merge 1 commit into
stleary:masterfrom
illusionoftalha:fix/null-subset-constructor-names

Conversation

@illusionoftalha

Copy link
Copy Markdown

Fixes #1096.

Both subset constructors dereference names.length in their constructor delegation, so a null names array fails before any fields can be selected. Treat null consistently with an empty names array: allocate an empty map and return without copying keys or reflecting over fields. Document that behaviour in both constructors.

Add regressions for null names with a populated JSONObject, null field names with a public-field object, and passing the null result of JSONObject.getNames on an empty object back to the subset constructor. The source JSONObject remains unchanged.

Validation on Windows / JDK 17:

  • All three new tests fail with NullPointerException before the source fix.
  • mvn test passes: 841 tests run, 0 failures/errors, 6 skipped.
  • git diff --check passes.

@sonarqubecloud

sonarqubecloud Bot commented Nov 1, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NullPointerException in JSONObject(JSONObject, String...) and JSONObject(Object, String...) when names is null

1 participant