gpt4 book ai didi

java.lang.NullPointerException : While displaying all my user files

转载 作者:行者123 更新时间:2023-12-02 04:25:23 24 4
gpt4 key购买 nike

我正在尝试使用 java 显示所有用户文件,如下所示...

 public static String Tree(File file)
{
if(file.isDirectory()==true)
{
File fr[]=file.listFiles();
No_fold++;
for(int i=0;i<fr.length;i++)
{
Tree(fr[i]);
}
}
else
{
X+=file.getPath()+"\n";
No_files++;
}
return X;
}

在显示我的文档文件时完美工作...但尝试显示 C:/Users/username

中的文件时会引发以下异常
at com.monster.app.__SHELL137.run(__SHELL137.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at bluej.runtime.ExecServer$3.run(ExecServer.java:725)

请帮我解决这个问题..谢谢你

最佳答案

File.listFiles() 的文档说,

An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

所以,listFiles()返回值为null的情况只有两种

  • 抽象路径名并不表示目录
  • 发生 I/O 错误

由于您要确保 file 是一个目录,我怀疑存在 I/O 错误。

你说过,

works perfectly while displaying my documents files... But the following exception is thrown while trying to display the files from C:/Users/username

我认为 C:/Users/username 或其子目录中的某些目录您无法读取并导致 I/O 错误。

关于java.lang.NullPointerException : While displaying all my user files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32255341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com