Set System property to Null in Java
Asked Answered
I

3

20

In my unit tests I need to set the "workingDir" system property to Null.

But I can not do somesing like this, because It give me NullPointerException:

System.setProperty("workingDir", null);

How can I do it?

Intercrop answered 1/12, 2010 at 11:46 Comment(0)
R
56

You can't set a property to have an actual null value - you can only clear it, like this:

System.clearProperty("workingDir");
Reduplicative answered 1/12, 2010 at 11:49 Comment(0)
S
5

System.setProperty() is internally implemented using a Properties object, which in turn uses the good old Hashtable. Those hashtables never let you set a null value using theput() method, so it can't really be done that way. Jon Skeet's post has the solution.

Subulate answered 1/12, 2010 at 11:50 Comment(0)
G
2

You can't do it, as setProperty method throws NullPointerException if any of it's arguments is null. You can put an empty string there and check for it in your unit tests or simply clearProperty, as Jon suggested.

Gribble answered 1/12, 2010 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.