我正在开发一个循环特定目录(例如 C:\Documents)并计算它在磁盘上占用的总大小的程序。但是,由于某种原因,我的程序在似乎在 Documents 文件夹中查找名为“My Music”的文件夹时不断抛出空指针异常。“我的音乐”在我的 Documents 文件夹中不存在,所以我很困惑它从哪里获取。我理解它为什么会抛出异常(显然,如果它在指定目录中找不到文件夹,它将返回 null),但我不知道它是如何找到“我的音乐”的。这是我的代码:
public static Long getDirSize(File directory) {
long size = 0L;
for (File file : directory.listFiles()) {
size += file.isDirectory() ? getDirSize(file) : file.length();
}
return size;
}
并调用此方法,我使用以下内容:
long required = 0;
for (int i = 0; i < directories.length; i++){
required = required + getDirSize(new File(directories[i]));
“目录”是一个字符串数组,其中包含我要计算其大小的目录。例如,目录 = {"C:\Users\user\Documents", "C:\Users\user\Pictures", "C:\Users\user\Videos"}
几个星期以来,我一直在尝试解决这个问题,尝试不同的方法来遍历目录等,它们似乎都给了我同样的问题。我将非常感谢另一组对此的看法。谢谢!
编辑:这是堆栈跟踪——
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)