{"record":{"id":"425b6c1fa230cac5","repo":"JetBrains/intellij-community","slug":"java-lang-nullpointerexception-cannot-unbox-null","errorCode":null,"errorMessage":"java.lang.NullPointerException: cannot unbox null value","messagePattern":"java\\.lang\\.NullPointerException: cannot unbox null value","errorType":"exception","errorClass":"EvaluateException","httpStatus":null,"severity":"error","filePath":"java/debugger/impl/src/com/intellij/debugger/engine/evaluation/expression/UnBoxingEvaluator.java","lineNumber":66,"sourceCode":"    TYPES_TO_CONVERSION_METHOD_MAP.put(CommonClassNames.JAVA_LANG_DOUBLE, Couple.of(\"doubleValue\", \"()D\"));\n  }\n\n  public static boolean isTypeUnboxable(String typeName) {\n    return TYPES_TO_CONVERSION_METHOD_MAP.containsKey(typeName);\n  }\n\n  public UnBoxingEvaluator(@NotNull Evaluator operand) {\n    myOperand = DisableGC.create(operand);\n  }\n\n  @Override\n  public Object evaluate(EvaluationContextImpl context) throws EvaluateException {\n    return unbox(myOperand.evaluate(context), context);\n  }\n\n  public static Object unbox(@Nullable Object value, EvaluationContext context) throws EvaluateException {\n    if (value == null) {\n      throw new EvaluateException(\"java.lang.NullPointerException: cannot unbox null value\");\n    }\n    if (value instanceof ObjectReference) {\n      final String valueTypeName = ((ObjectReference)value).type().name();\n      final Couple<String> pair = TYPES_TO_CONVERSION_METHOD_MAP.get(valueTypeName);\n      if (pair != null) {\n        return convertToPrimitive(context, (ObjectReference)value, pair.getFirst(), pair.getSecond());\n      }\n    }\n    return value;\n  }\n\n  private static Value convertToPrimitive(EvaluationContext context, ObjectReference value, final String conversionMethodName,\n                                          String conversionMethodSignature) throws EvaluateException {\n    // for speedup first try value field\n    Value primitiveValue = getInnerPrimitiveValue(value, true).join();\n    if (primitiveValue != null) {\n      return primitiveValue;\n    }","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/debugger/impl/src/com/intellij/debugger/engine/evaluation/expression/UnBoxingEvaluator.java#L48-L84","documentation":"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.","triggerScenarios":"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.","commonSituations":"Evaluating 'int x = nullableInteger;' at a breakpoint where nullableInteger is null; unboxing a null result of a method call; arithmetic like 'nullableLong + 1'.","solutions":["Null-check before unboxing: evaluate 'boxed != null ? boxed : 0'","Use explicit defaults: Objects.requireNonNullElse(boxed, 0)","Keep the expression in wrapper type (drop the primitive cast) until the value is confirmed non-null"],"exampleFix":"// before: int count = nullableCount;  // nullableCount is null\n\n// after: int count = nullableCount != null ? nullableCount : 0;","handlingStrategy":"type-guard","validationCode":"Object v = operand.evaluate(context);\nif (v == null) {\n  throw new EvaluateException(\"java.lang.NullPointerException: cannot unbox null value\");\n}\nreturn UnBoxingEvaluator.unbox(v, context);","typeGuard":"static boolean isUnboxable(Object value) {\n  return value != null; // and if ObjectReference: type is a registered wrapper\n}","tryCatchPattern":"try {\n  value = UnBoxingEvaluator.unbox(boxed, context);\n} catch (EvaluateException e) {\n  // treat as NPE: substitute a default primitive and warn\n}","preventionTips":["Null-check wrapper values before mixing them with primitives in fragments","Use ternary defaults: boxed != null ? boxed : 0","Keep intermediate values in wrapper type until null-ness is known"],"tags":["debugger","evaluation","unboxing","null-pointer"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}