aspose file tools*
The moose likes Beginning Java and the fly likes How to understand this NullPointerException? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to understand this NullPointerException?" Watch "How to understand this NullPointerException?" New topic
Author

How to understand this NullPointerException?

John-Philippe Verger
Greenhorn

Joined: Feb 18, 2008
Posts: 24
Hi,
I'm trying to write a program which outputs all the files on my machine(and their respective sizes).In the output, I want to have one line by file.
Here's the class AllFiles2.java I wrote for that.


The class AllFiles2 above compiles successfully.But when I execute the resulting class file, I have a NullPointerException. I don't understand why I have a NullPointerException.Here's the complete output I receive when I execute AllFiles2.

C:\ - Exception in thread "main" java.lang.NullPointerException
at AllFiles2.AllF(AllFiles2.java:8)
at AllFiles2.AllF(AllFiles2.java:15)
at AllFiles2.AllF(AllFiles2.java:15)
at AllFiles2.main(AllFiles2.java:31)

Note: I use Windows XP, and I have 2 partitions on my disk: C:\ and D:\.

Please, why, although the program is able to execute the line (at line30,in the for loop of the main method) and outputs'C:\ - ' , it sees at the line which follows(the line 31 ) that fi does not refer to an object?

Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

Start at the top of the stack trace:
at AllFiles2.AllF(AllFiles2.java:8)

It tells you that the exception was thrown at line 8 of that class, inside the "AllF" method. So find line 8; of course that refers to line 8 in your source code and not necessarily line 8 of what you posted, but it looks like you posted the entire class, in which case line 8 is this line:

This means that either the variable "f" is null, or else that its listFiles() method returns null.
John-Philippe Verger
Greenhorn

Joined: Feb 18, 2008
Posts: 24
Thanks, Paul Clapham, for your reply. After your reply, I inserted these two lines of code between the line 7 and the line 8 of the program shown in my first post.


The resulting source file compiles successfully, and when I execute the resulting class file, I get now:
[code] C:\ - f.listFiles() is null
Exception in thread "main" java.lang.NullPointerException
at AllFiles2.AllF(AllFiles2.java:8)
at AllFiles2.AllF(AllFiles2.java:15)
at AllFiles2.AllF(AllFiles2.java:15)
at AllFiles2.main(AllFiles2.java:31)[code]
But I don't understand why f.listFiles() is null, since my partition C:\ has lot of physical files and directories in it.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I would consider using the enhanced-for loop for iterating the files. I find it's more clear and in this case it causes that listfiles will only be invoked once vs theNumberOfFiles*2.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

John-Philippe Verger wrote:But I don't understand why f.listFiles() is null, since my partition C:\ has lot of physical files and directories in it.


I don't see any code which prints "C:\", so I don't know where that is coming from. And since listFiles() shouldn't be returning null if the File object actually represented that folder, you should consider the possibility that it doesn't represent that folder.

When you are debugging, don't get too attached to your theories about what is wrong. You have a theory that you are accessing an empty folder, since listFiles() returns null. And you have a theory that the folder in question is the root of your C drive -- for some reason that I don't understand. At least one of those theories is false, then. Personally, since the first theory is supported by the API documentation, I would tend to reject the second theory -- the one which claims that your C drive is empty.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2970
    
    9
I recommend adding some more debugging info:
John-Philippe Verger
Greenhorn

Joined: Feb 18, 2008
Posts: 24
Thanks Paul Clapham for your second reply. In your reply, you wrote:
I don't see any code which prints "C:\", so I don't know where that is coming from
.
Here's my answer:
In the output of the JVM I showed in my first post, the first line was:
.
That is why I think the is displayed by the line 30 of the source file I showed in my first post.And when I remove the lines 31, 32,33,34 and 35 of of the source file I showed in my first post, the source file is compiled successfully and the output when I launch the resulting class file is fine:
.
So, it seems like at line 29 in the the source file I showed in my first post, File.listRoots() is an array of two elements (the first element corresponding to the physical directory C:\ ,and the second directory corresponding to the physical directory D:\), and the program outputs C:\at line 30 of the source file I showed in my first post and for the first iteration in the for loop of the main method . So, fi at line 30 for the first iteration of the for loop of the main method refers to the physical directory C:\. Consequently, at line 31 for the first iteration of the for loop of the main method in the the source file I showed in my first post,the method AllF(fi) should give me all the files (physical files and physical directories) of the partition C:\, but it's like that is there that problems began.
Thanks.
John-Philippe Verger
Greenhorn

Joined: Feb 18, 2008
Posts: 24
HI Mike Simmons,

I add the all debugging lines of code you suggested between the line 7 of the source file of my first post(the line arr = new ArrayList<String>();) and the line 8 of the source file of my first post(the line for (int j = 0;j < f.listFiles().length;j++) {), and after having compiled and executed, I got the following output:
C:\ - Full path of f: C:\
Full path of f: C:\145c1fac119461cbd6927070f9
Full path of f: C:\145c1fac119461cbd6927070f9\amd64
Exception in thread "main" java.lang.NullPointerException
at AllFiles2.AllF(AllFiles2.java:14)
at AllFiles2.AllF(AllFiles2.java:21)
at AllFiles2.AllF(AllFiles2.java:21)
at AllFiles2.main(AllFiles2.java:37)

Thanks.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Question: Can a file have subfiles??

If you look at the JavaDoc for File at http://download.oracle.com/javase/6/docs/api/java/io/File.html#listFiles%28%29 you find this:


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.
Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

Hi John,

Could you please check whether you can access the last printed directory's contents, via explorer ?
I believe it's C:\145c1fac119461cbd6927070f9\amd64.
I'm getting the same nullpointer as you, but only on directories with access denied.

Otherwise, before the for loop, you may always check if (f.listFiles() !=null).

Cheers
Claudiu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to understand this NullPointerException?
 
Similar Threads
What is the input file?
ArrayList
SCJP 5: Question on Generics
CODE: Dynamic Sorting on Multiple Indexes
ArrayList of ArrayList<String>