Search code examples
exceptiondebuggingintellij-ideabreakpoints

Intellij Idea Java Debugging: How to create a line breakpoint that will only trigger a break if NullpointerException will be thrown at THIS line?


In the follow method, how could I create a line breakpoint at a.toString() to catch only NullPointerException at this line?

public static void m1(String b){
    String a = null;
    b.toString();
    a.toString();
}

I need to achieve:

  1. if b is null, it will not break at b.toString as only exception at the next line will trigger
  2. do not add conditions as there might be more than one variables that may cause exception
  3. Will break at a.toString, but only when a NullPointerException is about to occur. If there is no NullPointerException, will not break.

I have tried

enter image description here

and

enter image description here

or both of them combined, but it didnt break at all.

How to fix this?


Solution

  • your approach seems fine, just adjust it a little bit: line breakpoint should not suspend at all, it acts as a marker: enter image description here

    NPE breakpoint then will be enabled only after the debugger reached the line. Unfortunately there's no way to disable NPE breakpoint after that (only if it is hit)