Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify a List value while debugging in IntelliJ

I need to change a variable during debugging an application. Until now it was just basic variables which could directly be set. Now I need to clear an array so that isEmpty() returns true;

ArrayList<String> someList = new ArrayList<String>;
someList.add("1");
...
if(someList.isEmpty()){ //break point
//need to enter here
}

In the intellij debugger I see:

someList={ArrayList@4271} size=1

I used the 'setValue' method of the debugger and tried: new ArrayList<String>() or someList = new ArrayList<String>()

which results in

someList={ArrayList@4339} size=0

However if I continue I get a NullPointerException when the isEmpty() is called. So my question: How can I inject an empty ArrayList without getting a NPE?

The text of the NPe is: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.isEmpty()' on a null object reference

like image 651
Lonzak Avatar asked Apr 20 '16 13:04

Lonzak


People also ask

How do I change value while debugging?

Click on Window -> Open Perspective -> Debug. Click on Tab Variables. Right click the variable for which you want to change the value and click on Change Value... Set the Value as Boolean.

How do I skip parts of a program while debugging Intellij?

If you press and hold the Ctrl / ⌘ button while dragging the arrow, the IDE will highlight all the lines in green. When you drop the arrow, you won't jump to the line of code. Instead, the IDE will act as if you have used the Run to Cursor (⌥F9) action.


2 Answers

Did you try to use the "Evaluate expression" during debug ("Alt + F8" on Windows) ?

In this window you can write :

 someList.clear();

or

someList = new ArrayList<String>();

And it should do the trick.

like image 66
Guillaume M Avatar answered Oct 07 '22 22:10

Guillaume M


Stop the breakpoint at if(someList.isEmpty()), press ALT + F8 (evaluate expression), type someList.clear(), press Evaluate and just proceed on debugging. Now it will definitly enter the if condition.

like image 20
dambros Avatar answered Oct 07 '22 23:10

dambros