Set System propery to Null in Java

泄露秘密 提交于 2019-12-10 01:04:54

问题


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?


回答1:


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

System.clearProperty("workingDir");



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/4323995/set-system-propery-to-null-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!