ymyyyok
ymyyyok

Reputation: 31

How to use conditional breakpoint for foreach-loop in eclipse?

I want to know how to use a conditional breakpoint in eclipse. I have a code like:

for(A a:aList){}

I have already put a breakponit on the line and I have setted the condition

a.getXxx.equals("yyy")

but eclipse show me a error:

Conditional breakpoint has compliation error(s).
Reason:
a cannot be resolved

please help me find the reason.

Upvotes: 3

Views: 1958

Answers (1)

Korgen
Korgen

Reputation: 5399

you have to place the breakpoint in the first row within in the loop, as a will not be known on the line of the loop yet. So for

List<Object> myObjects = ...;
for (Object obj : myObjects ) {
    obj.doSth();
}

you would place the breakpoint on the line which is "obj.doSth();"

This is actually due to the fact that for the foreach loop the compiler does nothing else than a call to the Iterator.next(); method as the first statement in the loop (you wont notice that as the compiler does it automatically). Have a look at the java spec: http://docs.oracle.com/javase/specs/jls/se5.0/html/statements.html#14.14.2

Upvotes: 4

Related Questions