Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set System propery to Null in Java

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?

like image 953
Victoria Seniuk Avatar asked Dec 01 '10 11:12

Victoria Seniuk


People also ask

How do you set a null property in Java?

You can't do it, as setProperty method throws NullPointerException if any of it's arguments is null.

How do you set property to null?

To solve the "Cannot set property 'value' of null" error, make sure to insert the JS script tag at the bottom of the body. The JS script tag should be placed after the HTML elements have been declared.

Can you set something equal to null in Java?

Code Correctness: null Argument to equals()equals(null) will always be false. The program uses the equals() method to compare an object with null . This comparison will always return false, since the object is not null . (If the object is null , the program will throw a NullPointerException ).


2 Answers

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

System.clearProperty("workingDir");
like image 83
Jon Skeet Avatar answered Oct 13 '22 21:10

Jon Skeet


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.

like image 45
Martin Algesten Avatar answered Oct 13 '22 21:10

Martin Algesten