• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

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

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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




 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Petar Thomas
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
: )

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
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic