Tencent/APIJSON · error · NullPointerException

PUT {}, {} 类型为 {},不支持 Boolean, String, Number 等类型字段使用 'key+'

Error message

PUT {}, {} 类型为 {},不支持 Boolean, String, Number 等类型字段使用 'key+': [] 或 'key-': [] !对应字段在数据库的值必须为 JSONArray, JSONObject 中的一种!值为 JSONRequest 类型时传参必须是 'key+': [{'key': value, 'key2': value2}] 或 'key-': ['key', 'key2'] !

What it means

For PUT-style incremental updates with 'key+': [...] / 'key-': [...], the current database-side value (target) is fetched and, after an attempted JSON.parse for strings, must be a JSONArray or JSONObject. If it is Boolean/String-that-isn't-JSON/Number, a NullPointerException is thrown: you cannot add/remove array elements on a scalar column.

Source

Thrown at APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java:721


		//add all 或 remove all <<<<<<<<<<<<<<<<<<<<<<<<<
		Object target = rp == null ? null : rp.get(realKey);
		if (target instanceof String) {
			try {
				target = JSON.parse(target);
			} catch (Throwable e) {
				if (Log.DEBUG) {
					Log.e(TAG, "try {\n" +
							"\t\t\t\ttarget = parseJSON((String) target);\n" +
							"\t\t\t}\n" +
							"\t\t\tcatch (Throwable e) = " + e.getMessage());
				}
			}
		}

		if (apijson.JSON.isBoolOrNumOrStr(target)) {
			throw new NullPointerException("PUT " + path + ", " + realKey + " 类型为 " + target.getClass().getSimpleName() + ","
					+ "不支持 Boolean, String, Number 等类型字段使用 'key+': [] 或 'key-': [] !"
					+ "对应字段在数据库的值必须为 JSONArray, JSONObject 中的一种!"
					+ "值为 JSONRequest 类型时传参必须是 'key+': [{'key': value, 'key2': value2}] 或 'key-': ['key', 'key2'] !"
			);
		}

		boolean isAdd = putType == 1;

		Collection<Object> targetArray = target instanceof Collection ? (Collection<Object>) target : null;
		Map<String, ?> targetObj = target instanceof Map ? (Map<String, Object>) target : null;

		if (targetArray == null && targetObj == null) {
			if (isAdd == false) {
				throw new NullPointerException("PUT " + path + ", " + realKey + (target == null ? " 值为 null,不支持移除!"
						: " 类型为 " + target.getClass().getSimpleName() + ",不支持这样移除!")
						+ "对应字段在数据库的值必须为 JSONArray, JSONObject 中的一种,且 key- 移除时,本身的值不能为 null!"
						+ "值为 JSONRequest 类型时传参必须是 'key+': [{'key': value, 'key2': value2}] 或 'key-': ['key', 'key2'] !"
				);

View on GitHub (pinned to 5284052872)

Solutions

  1. Store the column as JSON (JSONArray/JSONObject) before using 'key+'/'key-' — migrate the data: 'hello' -> "[\"hello\"]".
  2. If the value is a JSON-encoded string, ensure it is valid JSON so JSON.parse succeeds before the check.
  3. For plain scalar columns, use regular 'key': value assignment instead of '+'/'-' modifiers.

Example fix

-- before: column tags = 'sport' (plain varchar)
-- after: UPDATE "User" SET tags = '["sport"]' WHERE id = 1;
// then PUT { "User": { "id": 1, "tags+": ["music"] } } works
Defensive patterns

Strategy: type-guard

Validate before calling

Object current = fetchColumnValue(tableName, id, key); // before composing the PUT
if (current != null && !(current instanceof Collection || current instanceof Map)) {
  throw new IllegalStateException(key + " is scalar (" + current.getClass().getSimpleName() + "); key+/key- require a JSON column");
}

Type guard

function isJsonCollection(v: unknown): boolean {
  if (typeof v === 'boolean' || typeof v === 'number' || v === null) return false;
  if (typeof v === 'string') { try { const p = JSON.parse(v); return Array.isArray(p) || p !== null && typeof p === 'object'; } catch { return false; } }
  return Array.isArray(v) || typeof v === 'object';
}

Prevention

When it happens

Trigger: PUT request with "tags+": ["a"] where the column 'tags' currently holds true, 123, or a plain string like "hello" (not parseable as JSON array/object).

Common situations: Schema drift: the column was scalar (VARCHAR) and later APIJSON 'key+'/'key-' syntax is used against it; data written by another system as plain text; strings that fail JSON.parse fall through to isBoolOrNumOrStr.

Related errors


AI-assisted analysis of Tencent/APIJSON@5284052872 (2026-08-14). Data as JSON: /api/errors/69b2a64bfc10e7d6. Report an issue: GitHub.