How to modify a List value while debugging in IntelliJ

Lonzak picture Lonzak · Apr 20, 2016 · Viewed 13k times · Source

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

Answer

Guillaume M picture Guillaume M · Apr 20, 2016

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.