JetBrains/intellij-community · error · EvaluateException

java.lang.NullPointerException: cannot unbox null value

Error message

java.lang.NullPointerException: cannot unbox null value

What it means

Thrown by UnBoxingEvaluator.unbox when an unboxing conversion is applied to a null value: the evaluated expression performs a primitive cast/conversion (e.g. assigning an Integer to int) but the wrapper object is null. The debugger reproduces the JVM's NullPointerException semantics with an explicit 'cannot unbox null value' message.

Source

Thrown at java/debugger/impl/src/com/intellij/debugger/engine/evaluation/expression/UnBoxingEvaluator.java:66

    TYPES_TO_CONVERSION_METHOD_MAP.put(CommonClassNames.JAVA_LANG_DOUBLE, Couple.of("doubleValue", "()D"));
  }

  public static boolean isTypeUnboxable(String typeName) {
    return TYPES_TO_CONVERSION_METHOD_MAP.containsKey(typeName);
  }

  public UnBoxingEvaluator(@NotNull Evaluator operand) {
    myOperand = DisableGC.create(operand);
  }

  @Override
  public Object evaluate(EvaluationContextImpl context) throws EvaluateException {
    return unbox(myOperand.evaluate(context), context);
  }

  public static Object unbox(@Nullable Object value, EvaluationContext context) throws EvaluateException {
    if (value == null) {
      throw new EvaluateException("java.lang.NullPointerException: cannot unbox null value");
    }
    if (value instanceof ObjectReference) {
      final String valueTypeName = ((ObjectReference)value).type().name();
      final Couple<String> pair = TYPES_TO_CONVERSION_METHOD_MAP.get(valueTypeName);
      if (pair != null) {
        return convertToPrimitive(context, (ObjectReference)value, pair.getFirst(), pair.getSecond());
      }
    }
    return value;
  }

  private static Value convertToPrimitive(EvaluationContext context, ObjectReference value, final String conversionMethodName,
                                          String conversionMethodSignature) throws EvaluateException {
    // for speedup first try value field
    Value primitiveValue = getInnerPrimitiveValue(value, true).join();
    if (primitiveValue != null) {
      return primitiveValue;
    }

View on GitHub (pinned to be881553f2)

Solutions

  1. Null-check before unboxing: evaluate 'boxed != null ? boxed : 0'
  2. Use explicit defaults: Objects.requireNonNullElse(boxed, 0)
  3. Keep the expression in wrapper type (drop the primitive cast) until the value is confirmed non-null

Example fix

// before: int count = nullableCount;  // nullableCount is null

// after: int count = nullableCount != null ? nullableCount : 0;
Defensive patterns

Strategy: type-guard

Validate before calling

Object v = operand.evaluate(context);
if (v == null) {
  throw new EvaluateException("java.lang.NullPointerException: cannot unbox null value");
}
return UnBoxingEvaluator.unbox(v, context);

Type guard

static boolean isUnboxable(Object value) {
  return value != null; // and if ObjectReference: type is a registered wrapper
}

Try / catch

try {
  value = UnBoxingEvaluator.unbox(boxed, context);
} catch (EvaluateException e) {
  // treat as NPE: substitute a default primitive and warn
}

Prevention

When it happens

Trigger: Evaluating '(int) boxedInteger' or an expression mixing wrapper and primitive types where myOperand.evaluate(context) returns null; value == null hits the first branch and EvaluateException('java.lang.NullPointerException: cannot unbox null value') is thrown. Also reached from boxing/unboxing conversions inserted by EvaluatorBuilder around assignments and casts.

Common situations: Evaluating 'int x = nullableInteger;' at a breakpoint where nullableInteger is null; unboxing a null result of a method call; arithmetic like 'nullableLong + 1'.

Related errors


AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14). Data as JSON: /api/errors/425b6c1fa230cac5. Report an issue: GitHub.