Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đang mở Phù hợp với người mới
#1,096 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
76/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
java
Lĩnh vực
backend

Hướng nghiên cứu

Đọc hai hàm khởi tạo bị ảnh hưởng trong src/main/java/org/json/JSONObject.java, tại các dòng được liên kết trong issue. Kiểm tra cách các lệnh gọi this(...) tường minh xử lý mảng names có giá trị null, sau đó chạy hoặc bổ sung kiểm thử tập trung cho cả hai phiên bản quá tải của hàm khởi tạo. Công việc được xem là hoàn tất khi cách xử lý null được chọn nhất quán, được ghi lại trong Javadoc và được xác minh cho cả hai hàm khởi tạo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
Ngôn ngữ chính
Java
Star
4.7k
Fork
2.6k
Merge trung bình
4 ngày 17 giờ
Pull request đã merge (30 ngày)
7

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của stleary/JSON-java

Tất cả issue của stleary/JSON-java

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.