Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

isDirectory command doubt ?  RSS feed

 
Anand Divakaran
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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?
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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() ?
 
Vinay Nag
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!