为什么这段代码会获得NPE?

Zac*_*tte -3 java

File f = new File(System.getProperty("user.home/Downloads"));
Run Code Online (Sandbox Code Playgroud)

为什么这段代码会出现NPE错误?

我知道它返回null,但目录在那里......

Jig*_*shi 6

因为

System.getProperty("user.home/Downloads")
Run Code Online (Sandbox Code Playgroud)

返回null,因为没有这样的关键设置user.home/Downloads

你可能正在寻找

final String fileName = System.getProperty("user.home") + File.saperator + "Downloads";
File f = new File(fileName);
Run Code Online (Sandbox Code Playgroud)

  • @ZachFollette:因为没有这样的财产.期. (4认同)