Java-空指针异常,文件输入?

发布于 2024-10-03 20:58:15 字数 872 浏览 3 评论 0原文

我正在尝试编写一个查找文件系统深度的方法和另一个查找最大文件的方法。我有几个文件夹,其中包含简单的文本文件,并且位于目录中的不同深度。我认为文件输入可能有问题,就像它没有接收任何内容或其他什么?这是堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException
at FileExploration.largestFile(FileExploration.java:68)
at FileExploration.main(FileExploration.java:14)

这是此跟踪中的行:

public static void main (String[] args) {
System.out.println(largestFile(new File(
    "c:/home/skatty14/workspace/LargestFile"))); //Line 14
}

File[] files = h.listFiles();
for (File f: files) { //Line 68
File lf = largestFile(f);

任何人都知道如何制作它,以便它可以正常工作?我怀疑第 14 行中的输入文件没有被正确“接收”或者什么?我不知道为什么,但是……如果有人能就这个问题提供任何线索,我们将不胜感激,谢谢!

编辑:哦,好吧...我想我可能已经找到问题了!?我使用的是 Linux:Ubuntu- Lucid Lynx,它甚至没有 ac:/ HD 目录。但是,如何从 Java 访问文件系统(特别是在主目录中)?

Edit2:已解决:我刚刚将目录更改为我的外部硬盘驱动器之一。也许权限阻止了对主文件夹的访问,我不知道......无论如何我以某种方式修复了它! ;)

I am trying to write a method that finds the depth of a file system and another that finds the largest file. I have several folders which contain simple text files and are at various depths within the directories. I think there might be a problem with the file input, like it's not taking anything in or something? Here is the stacktrace:

Exception in thread "main" java.lang.NullPointerException
at FileExploration.largestFile(FileExploration.java:68)
at FileExploration.main(FileExploration.java:14)

Here are the lines that are in this trace:

public static void main (String[] args) {
System.out.println(largestFile(new File(
    "c:/home/skatty14/workspace/LargestFile"))); //Line 14
}

File[] files = h.listFiles();
for (File f: files) { //Line 68
File lf = largestFile(f);

Anyone know how to make it so this will work correctly? I suspect that the input file in line 14 is not being "taken in" properly or something? I have no idea WHY, however...any light that someone can shed on this issue would be much appreciated, thanks!

Edit: Oh, ok...I think I might have found the problem!? I am using Linux: Ubuntu- Lucid Lynx, which doesn't even have a c:/ HD directory. Although, how do I access a filesystem from Java (in the Home directory in particular)?

Edit2: SOLVED: I just changed the directory to one of my external harddrives. Perhaps permissions were blocking access to the Home folder, I don't know...I fixed it somehow anyways! ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当梦初醒 2024-10-10 20:58:15

ubuntu 上主目录的路径; /主页/用户名/

Path to your home directory on ubuntu; /home/username/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文