The new version of the application has been released this morning and the client already notifies you about a bug… Adding an item in a list make the system crash. Ouch, you refactored this snippet of code a couple of days ago, you cannot pretend and blame
the trainee someone else. The good news is that you have reproduced the bug on your machine. Isn’t it said that a reproduced bug is a bug half corrected? Yes, well if only.
Ok, let’s start a code debugging session to determine the cause.
You realize that you iterate through a list and at a certain point, an object is null. And bam, the classic NullPointerException! You don’t have the time to look at the variables’ contents and understand why this object was null that the exception is already thrown. You’ll have to start again the debugging step by step until the critical point.
But I say stop! The IDE can help you!
You need to set a conditional breakpoint in order to stop only when the element to add is null.
Let’s consider a textbook case, you want to stop at the 98th iteration of a for loop.
Rather than pressing 97 times the “Continue” button, just add a condition, either directly on the loop or on one of the instructions as below.
The condition can be more complex and can be any Java code you can use in an if instruction.
The program will stop exactly where you want and especially when you want! Enjoy your debugging sessions!