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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With