Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.NullPointerException when trying to read files  RSS feed

 
Yotam Ohayon
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i have a little program here that gets a directory from the command line and adds all the files in this directory and its sub-folders.. so here's the code:

now when i run it on certain folders with not many files in them everything is okay, but when i feed it folders with a lot of files i get:


any idea as for why it happens? thanks!
p.s. i marked line numbers!
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
itr.listFiles() may return null even if its a directory, if you don't have read-access to the folder. This causes the dir parameter to be null, and then you try to iterate over it.

One small, unrelated piece of advice. Each recursive call creates a new ArrayList, which you discard immediately after. Since the method is private, you may want to change its signature to accept a List to add to. You then pass along the List from recursive call to recursive call:
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!