Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursive method to add all mp3 files on C:/  RSS feed

 
Ivan Marik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im having an issue with this bit of code. What Im trying to do is create an arraylist of (mp3) file objects and print it out to test that it is working properly. When I make a folder in my c drive and add some songs to it and set the str in the checkDir() to the folder path it works fine but when I set the str to my c drive itself I get a null pointer. Please help! Thanks!

 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Marik wrote:

listFiles may return null if the argument is not a directory, or if its contents cannot be read. At least one of the directories inside C: cannot be read - C:\System Volume Information. Your recursive code encounters this directory eventually. You then try to loop over a null File[], which is causing an NPE.

The solution - check if the result of listFiles is null before looping over it.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code will also recursively call checkDir for every file that does not end with "mp3". Considering the number of files on a typical Windows machine, this will seriously slow down your program.

I would also change the string your are comparing to ".mp3". There may be files with no extension whose name ends in "mp3" or there could be files with extensions such as .hdgsdjdfhmp3.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!