问题
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" 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!
EDIT: 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)
回答1:
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.
回答2:
I would try testing the file before actually using it. I would do something similar to if(file != null)
or if(file.exists())
. This way if the file doesn't exist you don't try to use it. This should solve your problem of getting an error.
Also, with the way Windows works, in your My Documents directory there is by default a folder called "Music", but depending on where you access this from in, say, explorer it might say "My Music" or "User's Music". I wasn't ever sure why this happened, but for some reason it does.
So a basically foolproof way to stop getting this error is to just check to see if the file you're about to use exists. If it doesn't, just skip over it or print an error message. Hope this helps.
来源:https://stackoverflow.com/questions/19968131/java-program-to-calculate-directory-size-keeps-throwing-npe