How to use conditions in breakpoints in idea?
Asked Answered
E

5

20

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.

Entreat answered 22/5, 2017 at 13:42 Comment(14)
can u put this condition in your code and see if its stopping there ?Plural
@AmitK The code snippet written in the logger is placed just before the breakpoint. It prints it correct, so I am sure the condition is fullfilled.Entreat
can you also provide your line of code, where you putting this condition breakpoint ?Plural
For testing purpose, could you store the value in a variable e.g int k = pdu.getVariables().size(); and simply put k==13 as a condition and check if this works ?Quodlibet
@Entreat , line on which you are putting the condition breakpoint is not having matching condition pdu.getVariables().size() , thats the reason its not stopping there.Plural
you can try if(pdu.getVariables().size()) { //do something } and put breakpoint on this line. it will stop therePlural
@AmitK So you mean that the expression to evaluate needs be the same as evaluated on the line?Entreat
@Entreat , Check that you have debug enabled , does it stops in this line without conditions ?Cuyp
@Entreat , no but at least expression should be there which can determine whether given condition is true or false. please see previous comment to understand it ore.Plural
@AmitK I tried to set the breakpoint on this line as well, and it does not work if (pdu.getVariables().size()==13) {;}Entreat
@MikeAdamenko It worked with the expression int k = pdu.getVariables().size(); k == 13;. Thanks! If you write it as an answer I will accept it.Entreat
@Entreat it means that your debugger wasn't enabled ?Plural
@Entreat you're welcome ) added as answer.Cuyp
@Entreat : maybe you were talking to me, so I added an answer . Tell me if I'm wrong .Quodlibet
T
20

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

Tradesman answered 22/5, 2017 at 13:56 Comment(6)
I looked at this help section as well. Actually it did not state any working examples which is a bit sad since the condition have to look something like this int k = pdu.getVariables().size(); k == 13;, which I see as extremely weird and possible a bug.Entreat
@Entreat multi-statements should add a return keyword. the code support java syntax. e.g: int k = pdu.getVariables().size(); return k == 13;Tradesman
@Entreat , i added similar condition and it stops for mePlural
@Entreat as I have said it is java code, maybe your condition throws a NullPointerException to interrupt. and I have test that condition not support lambda expression, so you need transform it with ternary operator statements. please try this pdu.getVariables()==null ? false : pdu.getVariables().size()==13;Tradesman
@Tradesman Yes I have looked a bit and it seems as if there can be a bug. I have tried some other stuff as well (why I have not been able to answer all comments.). I will consider removing this since the question can be seen as misleading.Entreat
@Entreat Essentially, the help documentation is already mentioned that is a java boolean expression and supports statements of multi-lines, except lambda expression is not has been introduced.Tradesman
C
2

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

Cenozoic answered 16/9, 2020 at 2:34 Comment(0)
P
0

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.

Plural answered 22/5, 2017 at 14:0 Comment(0)
C
0

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.

Cuyp answered 22/5, 2017 at 14:4 Comment(4)
Actually I mean that what was working was storing the result in a variable k in the breakpoint. So the breakpoint condition is int k = pdu.getVariables().size(); k == 13;. This works, but direct evaluation does not. It looks very much like a bug, but it is hard to say if it was intended.Entreat
Very strange, what version of IDEA are you using ?Cuyp
The bug can be result of some your plugins. Updated the answer.Cuyp
I am afraid I am unable to check the plugins. For the record, the semi-colon needs to be removed as well int k = pdu.getVariables().size(); k == 13. I have no idea why this happens. Thanks anywayEntreat
Q
-1

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 .

Quodlibet answered 22/5, 2017 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.