-1

Can someone tell me what are the possible reasons why a foreach loop in java is not executing? Because everytime I debug the program, it does not enter inside my foreach.

    for(SurveyReport surveyReport : surveyReportList)
    {
       System.out.println(surveyReport.getRiskRank().toString());
    }

This is my foreach loop. Simple but terribly weird because I'm always receiving InvocationTargetException.

Johan
  • 74,508
  • 24
  • 191
  • 319
Brilliant Robert
  • 539
  • 2
  • 5
  • 9
  • 8
    put the `for-each` loop code snippet first, else even we won't have any idea. – Rahul Mar 20 '13 at 07:35
  • If you'll add some code maybe we'll have an idea... – BobTheBuilder Mar 20 '13 at 07:36
  • foreach will not execute if in the example expression `for(int a: b){}` `b` is not Enumerable – Aniket Inge Mar 20 '13 at 07:36
  • For each wont run for empty collection. – BobTheBuilder Mar 20 '13 at 07:37
  • 2
    You have probably put a semi-colon after the ) and before the body of the loop starts! Or you are trying it on empty collection/array. – Sudhanshu Umalkar Mar 20 '13 at 07:38
  • 2
    @Aniket (1) There is no such thing as `Enumerable` in the JDK. (2) If `b` is not `Iterable` the code won't even compile, let alone execute. – user207421 Mar 20 '13 at 07:39
  • 1
    post your code here. so we have a starting point. – Drogba Mar 20 '13 at 07:41
  • @EJP 1) I did not mean `Enumerable` is a class/interface. I just meant that it cannot be enumerated, iterated on.. 2) I think he is getting an error during compilation - possibly – Aniket Inge Mar 20 '13 at 07:42
  • But I am getting the right size of my surveyReportList using surveyReportList.size(); – Brilliant Robert Mar 20 '13 at 07:44
  • Is this related to your problem? http://stackoverflow.com/questions/8958882/why-am-i-getting-an-invocationtargetexception-android-2d-game. Could you post the stack trace? – beatgammit Mar 20 '13 at 07:45
  • 1
    @BrilliantRobert, the stack trace associated with the InvocationTargetException will likely contain the answer to your question. – Tim Bender Mar 20 '13 at 07:47
  • @Aniket When you say a loop cannot be enumerated when what you mean is that it cannot be iterated, or when you say a loop won't execute when what you mean is that it won't compile, the overwhelming presumption is that you don't know what you're talking about. If you don't want to be misunderstood, use the right words. It's not difficult. – user207421 Mar 20 '13 at 08:59
  • @EJP the question since I posted the comment was edited. OP first said "b is not enumerable" posting a sample code. Then he changed to the way it looks right now. – Aniket Inge Mar 20 '13 at 09:01
  • @Aniket And you made the same error, and another error about executable vs compilable, and I am commenting on that. – user207421 Mar 20 '13 at 09:05

2 Answers2

2
  • The collection being iterated over is empty
  • Program flow circumvents the for-each loop (exception, conditional, etc...)
  • A misplaced ; results in an empty for-each loop body statement
Tim Bender
  • 20,112
  • 2
  • 49
  • 58
1

"InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor." java.lang.reflect.InvocationTargetException

This suggests that the code may be entering the loop, but if so the first println argument expression involves reflection, and a method or constructor that is being invoked through reflection throws an exception.

It may be failing before getting into the loop but after the last place you checked it reached.

Try catching the exception and displaying its getCause() to see what is really going wrong.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
  • When I print the message, the exception says "[Ljava.lang.Object; cannot be cast to com.hris.eis.entity.survey.SurveyReport" – Brilliant Robert Mar 20 '13 at 08:35
  • Where is the exception being thrown? Look through the stack trace to find the deepest call from code you recognize, and see what is going on at that line number. – Patricia Shanahan Mar 20 '13 at 08:56