19

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

Lonzak
  • 9,334
  • 5
  • 57
  • 88

3 Answers3

27

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.

Lonzak
  • 9,334
  • 5
  • 57
  • 88
Guillaume M
  • 470
  • 1
  • 5
  • 12
7

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.

dambros
  • 4,252
  • 1
  • 23
  • 39
0

I have this issue, but I am using Kotlin, which you can do (someList as ArrayList<*>).clear()

Xianwei
  • 2,381
  • 2
  • 22
  • 26