计算目录大小的Java程序不断投掷NPE [英] Java program to calculate directory size keeps throwing NPE

查看:88
本文介绍了计算目录大小的Java程序不断投掷NPE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个通过特定目录(例如C:\Documents)循环的程序,并计算它在磁盘上占用的总大小。然而,由于某些原因,我的程序在文档文件夹中似乎正在寻找一个名为我的音乐的文件夹时,会抛出一个空指针异常。 我的音乐不存在于我的文档文件夹中,所以我很难理解它在哪里。我明白为什么它抛出异常(显然,如果找不到指定目录下的文件夹,它将返回null),但我不知道它如何找到我的音乐首先。这是我的代码:

I'm working on a program that loops through a specific directory (example, C:\Documents) and calculates the total size taken up by it on the disk. For some reason, however, my program keeps throwing a null pointer exception at a point where it seems to be looking for a folder called "My Music" within the Documents folder. "My Music" does not exist in my Documents folder, so I am very confused about where it gets it from. I understand why it's throwing the exception (obviously if it can't find the folder within the designated directory, it'll return null), but I have no idea how it finds "My Music" in the first place. Here is the code I have:

public static Long getDirSize(File directory) {
    long size = 0L;
    for (File file : directory.listFiles()) {
        size += file.isDirectory() ? getDirSize(file) : file.length();
    }
    return size;
}

要调用此方法,我使用以下内容:

And to call this method, I use the following:

long required = 0;
for (int i = 0; i < directories.length; i++){
required = required + getDirSize(new File(directories[i]));

目录是一个包含我正在尝试计算大小的目录的字符串数组。例如,
directories = {C:\Users\user\Documents,
C:\Users\user\Pictures,
C:\\ \\ Users\user\Videos}

"directories" is an array of strings containing the directories whose size I'm trying to calculate. For example, directories = {"C:\Users\user\Documents", "C:\Users\user\Pictures", "C:\Users\user\Videos"}

我一直在努力解决这几个星期,尝试不同的方法循环浏览目录等,他们都似乎给我同样的问题。我非常欣赏另一套眼睛。谢谢!

I've been trying to solve this for several weeks, trying different methods to loop through the directories, etc and they all seem to give me the same problems. I would very much appreciate another set of eyes on this. Thanks!

编辑:这是stacktrace -

Here's the stacktrace--

java.lang.NullPointerException
at diana.Review.getDirSize(Review.java:206)
at diana.Review.getDirSize(Review.java:207)
at diana.Review$2.actionPerformed(Review.java:126)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


推荐答案

listFiles()可以返回null。见Javadoc。您需要在循环之前测试。这意味着你不能使用增强型for循环。

listFiles() can return null. See the Javadoc. You need to test for that before you loop. That means you can't use the enhanced for-loop on it.

这篇关于计算目录大小的Java程序不断投掷NPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆