Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

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

Abierto Apto para principiantes
#1,096 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
76/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
java
Área
backend

Línea de trabajo

Lee los dos constructores afectados en src/main/java/org/json/JSONObject.java, en las líneas enlazadas en la issue. Comprueba cómo gestionan sus llamadas explícitas a this(...) un array de nombres null y ejecuta o añade pruebas específicas para ambas sobrecargas del constructor. Se considera terminado cuando el comportamiento para null se haya elegido de forma coherente, esté documentado en Javadoc y se haya verificado para ambos constructores.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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
Lenguaje dominante
Java
Estrellas
4.7k
Forks
2.6k
Merge medio
4 d 17 h
PR fusionados (30 d)
7

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de stleary/JSON-java

Todos los issues de stleary/JSON-java

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.