*
The moose likes Java in General and the fly likes Null Pointer Exception...Why?  I need help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Null Pointer Exception...Why?  I need help." Watch "Null Pointer Exception...Why?  I need help." New topic
Author

Null Pointer Exception...Why? I need help.

Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Here is the code, i will put remark where is a NullPonerException:



This program should , by my belife, iterate over the whole file system on my computer, and, on my suprise, it does over all items in the root directory, and it even goes further somewhere, where I tried to follow this program with debugger, but with my limited knowledge of how to use the debugger, on Eclipse, I didn't figured out why this program have fallen apart by the NullPointerException on the given marked line.

If I put that line in a try/catch block, then I get as an output few hundart times printed stack trace, not much more, and a number 411288, which is obviously a close number to how much files there is on my computer:



By the way, I'm on Vista.



I can't solve this problem using debugger becaouse I am a begginer in using debugger and becaouse when I put a break line overthere where a NullPointerException ... arises... I end up clicking resume on that place for many times before the first NullPointerException and then when it happenes the program is over and I cant get variables...

Maybe I am suppose to put some code inside catch block that will read to me something saved from the last iteration without exception, from where I could read exact place (file) where I'm stuck.

Maybe someone knows why?

What is this?

(I think that possibly I'm over for today. Some nice days...

Thank You




Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I suspect the stack trace is actually pointing to the line before the one you've indicated. File.listFiles() can return null if there's an I/O error; I think that includes security issues -- i.e., if "f" is a directory to which you do not have read access, listFiles() will return null. You need to check for that null, and that should fix your problem. I believe you should be able to use File.canRead() to see if you have read permission first; if canRead() returns false, listFiles() would return null, so you can just skip that directory.

This program is going to eat an awful lot of memory -- why do you need to use a new CompositeDirectory at each recursive call?


[Jess in Action][AskingGoodQuestions]
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
: )

Thank You, thank you.

1) Yes. stack trace was pointing one line before than what I indicated.




Isn't it that, if there's no real array, just null, that then there shouldn't be no iteration as well?
So there is (at least one iteration) with f1 seted to null... One day I shall understand that.


2) Like this it works:



output : ))))




3) But, like this it doesn't work:



I don't know why it doesn't work. Maybe it cares only for files, maybe it works like that only on unix, maybe it has something to do with the security manager, I don't know for now. I readed javadoc, probably File.canRead() is only for files


4) I am not doing anything practical. I am learning. After all, I am allowed to clog my own computer alone. : )))) I was learning composite pattern. I will invent later some practical usage, maybe... : )))


Thank You very much for Your kind help...

p.s. I found one song from Kylie Minogue that I liked so much that I have listened it for many times now. I'm listening it right now. The name of the song is "Stay this way", from Kylie Minogue. It's good song, if anyone likes:
http://www.youtube.com/watch?v=0E4qliulvkY

I wish to You all people well.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Petar Tomičić wrote:


Isn't it that, if there's no real array, just null, that then there shouldn't be no iteration as well?
So there is (at least one iteration) with f1 seted to null... One day I shall understand that.


That would be nice, but unfortunately they didn't define it that way. If "list" is null, then there's a null pointer exception when the code tries to get the first File.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null Pointer Exception...Why? I need help.
 
Similar Threads
JTree and Filesystem?
Recursive File List - Help me problem solve please
traverse?
Scan through the entire workspace for specific directories in JAVA
Browsing a directory structure