Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

オープン 初心者向け
#1,096 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
java
領域
backend

調査の方向性

issue でリンクされている行にある、src/main/java/org/json/JSONObject.java の該当する2つのコンストラクターを読んでください。明示的な this(...) 呼び出しが null の names 配列をどのように扱うかを確認し、両方のコンストラクターオーバーロードを対象としたテストを実行するか追加してください。null に対する動作が一貫して選択され、Javadoc に記載され、両方のコンストラクターで検証されたら完了です。

索引モデルが issue の本文から書いたものです。

説明

Awaiting further input from submitter

https://github.com/stleary/JSON-java/blob/69383f746d971fd26bb4fe21524295dc385d233f/src/main/java/org/json/JSONObject.java#L183

Description

Both "subset" constructors dereference names.length in their explicit constructor call (this(names.length)) before any validation, so passing a null array throws a raw NullPointerException instead of a meaningful error (or being treated as "no names").

Affected constructors:

  • JSONObject(JSONObject jo, String... names) - JSONObject.java:183
  • JSONObject(Object object, String... names) - JSONObject.java:491

Because the dereference happens inside this(...), it cannot be guarded with a null check or try/catch within the constructor body. The try/catch inside the loop of the first constructor does not help either, since the NPE is thrown before the loop is reached.

Steps to reproduce
JSONObject jo = new JSONObject().put("a", 1);

new JSONObject(jo, (String[]) null);                 // NPE at JSONObject.java:183
new JSONObject(new Object(), (String[]) null);       // NPE at JSONObject.

This can happen in practice when the array comes from a variable or a meth returns null (an omitted varargs argument is an empty array, but an explicit null is passed through as null).

Actual behavior

java.lang.NullPointerException: Cannot read the array length because "name at org.json.JSONObject.<init>(JSONObject.java:183)

Expected behavior

One of the following, applied consistently and documented in the Javadoc:

1. Treat null like an empty array and return an empty JSONObject, or
2. Throw an explicit NullPointerException / IllegalArgumentException with message (e.g. "names must not be null").

A possible minimal fix is to avoid dereferencing names in the this(...) call, e.g.:

this(names == null ? 0 : names.length);
if (names == null) {
    return;
}

Environment

- org.json:json: 20260814 (also present in earlier versions, e.g. 20250517
- Java: OpenJDK 21.0.8
- OS: Linux
主要言語
Java
スター
4.7k
フォーク
2.6k
平均マージ
4日 17時間
マージ済み PR(30日)
7

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

stleary/JSON-java のほかの issue

stleary/JSON-java の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。