Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Set Conditional Breakpoints in IDEA

DZone's Guide to

Set Conditional Breakpoints in IDEA

What I really wanted to do was set a breakpoint and examine the state of the objects at runtime.

· Web Dev Zone
Free Resource

Make the transition to Node.js if you are a Java, PHP, Rails or .NET developer with these resources to help jumpstart your Node.js knowledge plus pick up some development tips.  Brought to you in partnership with IBM.

So yesterday I was attempting to debug an issue in a batch processing module within one of our applications. In short, an assertion was failing deep within Hibernate as it attempted to flush the session. Using a combination of various log statements, I had isolated the problem down to a particular record that the batch process was attempting to update. (BTW: I know you shouldn't be using Hibernate for batch processing - however, we're talking about batches of at most 1000 records here, not millions!) What I really wanted to do was set a breakpoint and examine the state of the objects at runtime; however, I dreaded the thought of clicking through the breakpoint time and time again until I got to the particular record that was causing the problem. "Surely," I thought, "there must be a way to tell the debugger to only break under certain conditions."

So, here's the code I wanted to examine:

public Publication parsePublication(String inputLine) throws ParseException {        Publication publication = new Publication();        String[] fields = inputLine.split("\t");        publication.setPublicationType(fields[0]);        ...        return publication;} 

Essentially, I wanted to break after inputLine.split("\t"); if and only if fields[35] existed and was equal to "PM:16732581." After examining IDEA's Breakpoint dialog, I noticed a section in the bottom right-hand corner that I'd never played with before:


[img_assist|nid=777|title=|desc=|link=none|align=middle|width=634|height=673]

Asit turns out, this is exactly what I needed. If you click on theellipsis next to the drop menu, you get a context-sensitive editorequipped with code completion:

[img_assist|nid=778|title=|desc=|link=none|align=middle|width=416|height=233]

Enter the desired conditions and voila! A conditional breakpoint. It worked like a charm the very first time, and I only had to inspect the breakpoint when the problematic record came up.

Another nice feature of the conditional breakpoint is that if some sort of exception (such as a NullPointerException) occurs while attempting to evaluate the conditional expression, IDEA pops up a dialog informing you what happened and asking if you want to stop at the breakpoint or continue. Nice.

Learn why developers are gravitating towards Node and its ability to retain and leverage the skills of JavaScript developers and the ability to deliver projects faster than other languages can.  Brought to you in partnership with IBM.

Topics:
jetbrains

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}