• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Recursive method to add all mp3 files on C:/

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic