• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Traversing directory and printing contents of all child directories throws NullPointerException

 
Ranch Hand
Posts: 109
1
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basic idea; crawl my mp3 collection in a quick and dirty way and print the filenames of my mp3s in a text file.

So far so good, I have the same structure of artists and albums for almost all of my mp3s. It goes:

Amon Amarth -> Deceiver of the Gods -> mp3s



I know there are some cases that the structure goes:

Artist -> Album Name -> CD1 -> mp3s

but this shouldn't be a major problem.

When I run the program, it does create a file and save contents but at some point the program crashes with a NullPointerException leaving the file incomplete. If I remove the album that the program crashes into, it will crash at some other point. If I test the program in a much smaller root directory, it will finish no problem. My mp3 collection directory is 60GB of mp3 counting at ~8000mp3 files.

My quick and dirty way follows:
 
Marshal
Posts: 27372
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vasilis Souvatzis wrote:at some point the program crashes with a NullPointerException



Okay. Then the first thing you should do is to find out what point that is. Look at your stack trace and it will tell you what line of code threw the exception.

If you can't figure out what the stack trace is telling you, no problem, copy it and paste it in a response and we'll help you out with that.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you call xxx.listFiles() you should first check to ensure that xxx is a directory. And after you call it, you should check to ensure that the return value is not null.
 
Vasilis Souvatzis
Ranch Hand
Posts: 109
1
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not much of a stacktrace really, it doesn't print anything useful, the error happens at line 25 of my code above.

I'll test what Fred said, I peeked at the File API and it does say that if the listFiles() method doesn't have directories, it returns null. It must be that, my checks must be reversed.

Be right back.
 
Marshal
Posts: 76464
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vasilis Souvatzis wrote:Not much of a stacktrace really, it doesn't print anything useful, . . .

Of course it prints something useful. Stack traces are always useful. It has told you the line number; please copy that line because it gives you a hint of what is null.
 
Vasilis Souvatzis
Ranch Hand
Posts: 109
1
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fair enough



The line is the 25 in my above code. Clearly at some point the albumContent is null.

What baffles me is this though; in my Metal directory it will NPE. In a Metal2 directory (much smaller than Metal) it will traverse the dirs correctly and won't NPE. That's what I don't get. It works in one root directory and not in another.
 
Vasilis Souvatzis
Ranch Hand
Posts: 109
1
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All right so: I decided to change my implementation and use the nio API in case it threw better exceptions than using File arrays. It did!

It turns out, it was the bloody desktop.ini files that caused the problem. I don't know why or how but I guess they didn't count as "files" maybe? After I removed the "problematic" albums, it traversed my collection just fine.

 
Paul Clapham
Marshal
Posts: 27372
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There you go... you assumed that something in your file system was either a file or a directory, but then you got stabbed in the back by Windows. By the way there's a File.isFile() method which might work more reliably for determining if something is a file, but I haven't tested it on your test case so I could be wrong.
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic