• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

isDirectory command doubt ?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my situation.

i'm trying to locate a file in a directory , when i identify a file object as a directory i use the String[] list() method defined in File class but it oftens throws NULLPOINTEREXCEPTION !

if (fileobj.isDirectory())
String s[] = fileobj.list();

it gives me nullpointerexception how come?
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably because you never initialized fileobj to be an actual File object?

By the way, do not trust that list() will return a valid array. From the API:

Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.


If your directory cannot be read for some reason, such as file level permissions that block you from opening the folder, it will return null without a doubt.

The same goes for listFiles().
 
Anand Divakaran
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm writing a program wherein when i specify the filename it searches in all the available drives for a match.

is their an alternative way to do it , other than by using list() ?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is one more method listFiles() but returns a file[], which might come into use. but as Rob as already mentioned, if the File object is null. any method call on that will give a null pointer.
 
reply
    Bookmark Topic Watch Topic
  • New Topic