patrik
patrik

Reputation: 4558

How to use conditions in breakpoints in idea?

I have tried this back and forth for a longer time, but somehow it does not work.

enter image description here

I have also tried with and without semicolon. I have also tried to run this line right before the breakpoint to make sure the condition really works,

logger.error("I am Here! " + "#PDU Elements: " + pdu.getVariables().size());

and it returns 13 as expected.

Does anyone know how to get this to work?

EDIT

On request I will add the lines of code run, at the breakpoint,

logger.error("I am Here! " + "#PDU Elements: " + pdu.getVariables().size());
Trap trap = Trap.createTrapFrom(variableMap, instanceIdentificationNumber); // Breakpoint in margin on this line.

EDIT 2

The problem seems to be related to that IDEA randomly misses some breakpoints. I have also tried some unconditional breakpoints (should always stop) and these only stops at specific times.

Upvotes: 20

Views: 27169

Answers (5)

D. Naumov
D. Naumov

Reputation: 327

Just click Right Mouse Button on your breakpoint

enter image description here

CTRL+Shift+F8 or CMD+Shift+F8 to view all active breakpoints enter image description here

Upvotes: 2

holi-java
holi-java

Reputation: 30676

press CTRL+SHIFT+F8 twice quickly at your breakpoints will open a dialog not a popup dialog to configure a condition. then press F1 to opening the helping dialog.

as intellij help documentation says a breakpoint condition is:

Select this check box and specify a condition for hitting a breakpoint in the text field. A condition is a Java Boolean expression (including a method returning true or false), for example, str1.equals(str2). This expression should be valid at the line where the breakpoint is set, and is evaluated every time the breakpoint is reached. If the evaluation result is true, user-selected actions are performed. If the result is false, the breakpoint does not produce any effect. If the Debugger cannot evaluate the expression, it displays the Condition evaluation error message. You can select whether you would like to stop at this breakpoint or ignore it. Conditions for field/method/exception breakpoints are calculated in the context for the given field/method/exception. To the right of the Condition field, there is the button (Shift+Enter) that opens the multiline editor.

Note

breakpoint condition is consist with java code, so any error occurs in condition will stop at the breakpoint. and it does not supports any lambda expressions. when you calculate the condition with multi-statements you need using return statement to return the result.

AND the condition often throws NullPointerException to stop the breakpoint. you need check null in breakpoint condition:

//change the condition
pdu.getVariables().size() == 13
                  ^-----throws a NullPointerException if variables is null

//to the condition using ternary operator for checking null
pdu.getVariables()==null ? false : pdu.getVariables().size()==13

Examples

for example:

private String[] run(Class<?> mainClass
     , Optional<String> launcherClass, String[] args) {
    ...
    ^-----I mark a breakpoint here
}

my condition is and remeber to check the condition checkbox:

launcherClass != null

My Breakpoint Condition Screenshot

enter image description here

Upvotes: 20

Mike Adamenko
Mike Adamenko

Reputation: 3002

You should check that you have debug enabled and it stops in this line without conditions.

Check the mute breakpoints button in the Intellij IDEA.

UPDATED

Check the installed plugins. Try to disable some of them.

Upvotes: 0

Arnaud
Arnaud

Reputation: 17524

There may be better ways to do what you want, but at least you could store the value in a variable in your code, e.g :

int k = pdu.getVariables().size();

then use its value :

k==13 

as a condition for your breakpoint .

Upvotes: -1

Amit
Amit

Reputation: 32376

You need to put conditional breakpoint at a line which can evaluate your given condition on that line , if that line can't evaluate that condition then your debugger will never stop at that line.

For example :-

public class DebbuerEx {

    public static void main(String[] args) {
        HashMap<String,String > map = new HashMap<>();
        map.put("Amit","k");
        map.put("Jaipur","Rajasthan");
        if(true) {
            //map.remove("Amit");
        }
        for(Map.Entry<String,String> entry : map.entrySet() ) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }
}

In Above code if i put a conditional breakpoint like below screen-shot it will never stop.

enter image description here

But if change the if condition to if(map.containsKey("Amit")) then same conditional breakpoint will stop there.

Upvotes: 0

Related Questions