{"record":{"id":"5f98838e61e74f1d","repo":"JetBrains/intellij-community","slug":"unable-to-do-foreach-for","errorCode":null,"errorMessage":"Unable to do foreach for","messagePattern":"Unable to do foreach for","errorType":"exception","errorClass":"EvaluateException","httpStatus":null,"severity":"error","filePath":"java/debugger/impl/src/com/intellij/debugger/engine/evaluation/expression/ForeachStatementEvaluator.java","lineNumber":32,"sourceCode":"  private final Evaluator myIterableEvaluator;\n  private final Evaluator myBodyEvaluator;\n  private final String myLabelName;\n\n  public ForeachStatementEvaluator(Evaluator iterationParameterEvaluator,\n                                   Evaluator iterableEvaluator,\n                                   Evaluator bodyEvaluator,\n                                   String labelName) {\n    myIterationParameterEvaluator = iterationParameterEvaluator;\n    myIterableEvaluator = DisableGC.create(iterableEvaluator);\n    myBodyEvaluator = bodyEvaluator;\n    myLabelName = labelName;\n  }\n\n  @Override\n  public Object evaluate(EvaluationContextImpl context) throws EvaluateException {\n    final Object iterable = myIterableEvaluator.evaluate(context);\n    if (!(iterable instanceof ObjectReference)) {\n      throw new EvaluateException(\"Unable to do foreach for\" + iterable);\n    }\n\n    if (iterable instanceof ArrayReference) {\n      return new ForStatementEvaluatorBase(myLabelName, myBodyEvaluator) {\n        private int myCurrentIndex = 0;\n        private int myArrayLength = -1;\n        private Evaluator myNextEvaluator;\n\n        @Override\n        protected Object evaluateInitialization(EvaluationContextImpl context, Object value) throws EvaluateException {\n          myArrayLength = ((ArrayReference)iterable).length();\n          myNextEvaluator = new AssignmentEvaluator(myIterationParameterEvaluator,\n                                                    new Evaluator() {\n                                                      @Override\n                                                      public Object evaluate(EvaluationContextImpl context) {\n                                                        return ((ArrayReference)iterable).getValue(myCurrentIndex++);\n                                                      }\n                                                    });","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/debugger/impl/src/com/intellij/debugger/engine/evaluation/expression/ForeachStatementEvaluator.java#L14-L50","documentation":"Thrown by ForeachStatementEvaluator.evaluate when the iterable expression of an enhanced-for loop in the evaluated fragment evaluates to something that is not an ObjectReference in the debuggee VM (null, a raw primitive, a void result). Without an ObjectReference (ArrayReference or Iterable object) the foreach machinery cannot iterate, so it aborts. Note the missing space in the original message concatenation.","triggerScenarios":"Evaluating 'for (x : expr) {...}' where myIterableEvaluator.evaluate(context) returns null (NPE in the evaluated provider, method returned void) or a PrimitiveValue; instanceof ObjectReference fails and EvaluateException('Unable to do foreach for' + iterable) is thrown.","commonSituations":"Evaluating a foreach over a variable that is null at the breakpoint; iterating over a primitive-typed expression; the iterable's getter threw inside the debuggee and the evaluator propagated null; evaluating a foreach over a Map instead of map.entrySet().","solutions":["Check the iterable is non-null and is an array or Iterable before the loop: for (x : Objects.requireNonNull(list))","For maps, iterate map.entrySet() or map.keySet() instead of the map itself","Step the debugger to a point where the collection is initialized, then evaluate"],"exampleFix":"// before: for (String s : names) { ... }  // names is null\n\n// after: for (String s : java.util.List.of(\"a\")) { ... }\n// or evaluate later, after names is assigned","handlingStrategy":"validation","validationCode":"Object iterable = iterableEvaluator.evaluate(context);\nif (!(iterable instanceof ObjectReference)) {\n  throw new EvaluateException(\"Unable to do foreach for \" + iterable);\n}\n// guard before: check the collection variable is non-null at this breakpoint","typeGuard":"static boolean isForeachable(Object evaluated) {\n  return evaluated instanceof ObjectReference; // ArrayReference or Iterable mirror\n}","tryCatchPattern":"try {\n  foreachEvaluator.evaluate(context);\n} catch (EvaluateException e) {\n  // inspect the iterable expression alone; if null, skip the loop\n}","preventionTips":["Evaluate the collection expression alone and confirm it is non-null before foreach","Iterate map.entrySet()/keySet(), not the Map itself","Prefer indexed for-loops over possibly-null iterables in fragments"],"tags":["debugger","evaluation","foreach","iterable"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}