• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

How to understand this NullPointerException?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28000
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Marshal
Posts: 28000
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Master Rancher
Posts: 4661
63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recommend adding some more debugging info:
 
John-Philippe Verger
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 75
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic