• Post Reply Bookmark Topic Watch Topic
  • New Topic

File and directory  RSS feed

 
Anders Kviback
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I have a problem when I'm trying to create a path to a map and its files. I have a class Directory which implements the interface DirectoryListing.


And class Directory


To run this code I have a class Uppgift1.


But when I run Uppgift1 I get a NullPointerException at line 59 in class Directory ie
if (katalog[i].isDirectory()){

So, the program can't find any objects here, but what am I doing wrong here?
As you understand this is an assignment but I don't feel so comfortable with the constructors and the set - methods. The arguments in the constructors and in set methods are fix.
Can anyone please help me here?

Anders
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have created a katalog[] array, but not initialised its members. Each array member defaults to null.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anders Kviback wrote:

When you create temp in both occurrences, it will contain only null values. The first line in both occurrences is not a problem; the second though will overwrite the original array (filer, katalog) with temp. Since temp only has one element set so far, all other elements of filer / katalog are null. When accessing the second element you will get a NullPointerException.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You have created a katalog[] array, but not initialised its members. Each array member defaults to null.

Did you miss line 54 which declares and initializes the array?

Anders, you should check out the overridden listFiles method that takes a FileFilter. For getting the files you use an implementation that accepts files if they are actual files (isFile() returns true), for getting the directories you use an implementation that accepts files if they are directories (isDirectory() returns true). That way you don't need to copy anything anymore, the API will do the hard work for you.
 
Anders Kviback
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for answering,

I've changed the code and it now looks like this in the two methods getDirectories() and getFiles().


But now I get NPE at method list instead. But I've changed that code as well :


But now I get the NPE at line 8 in the list() method. I e when I try to print it out. Do you understand why?

Anders
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are creating an array of files (not necessarily to be directories) in the getDirectories() method and assign it to katalog array.

You try to create a new array temp with the same size as the katalog.

Now when you put elements into the temp array, you check if it's a directory. Only if it's a directory, the array elements of temp are assigned a value.

There might not be any directory and hence the File references of temp array points to null and hence the Exception occurs when you try to access temp[i] and call a File method on a null reference.
 
Anders Kviback
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You say :

You are creating an array of files (not necessarily to be directories) in the getDirectories() method and assign it to katalog array.



Why don't they turn to directories when I use the method isDirectory()?

So you mean that I should do it this way:


Now, katalog has the correct size or?
But I get an NPE in the list method. Here:


What is wrong here???

Rob Spoor suggested to use an accept method from FileFilter but I don't understand how to do that??



 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's first focus on your NPE.
Assume that listFiles() returns the following:
- folder A
- folder B
- file C
- folder D
- file E
- file F

Your code will create the following arrays:
filer:
- null
- null
- file C
- null
- file E
- file F

katalogs:
- folder A
- folder B
- null
- folder D
- null
- null

As you can see, each has a null where the other has a file. There are ways to work around that (e.g. use a List<File> or use a separate counter for the destination array, then use Arrays.copyOf to "shrink" the array), but I'd go for the FileFilter solution.

File.listFiles is overloaded to take an instance of a class that implements java.io.FileFilter. This interface has one single method: public boolean accept(File file). When used with File.listFiles, the returned File[] will include only those File objects for which this one method returns true.
So now all you need to do is create an implementation and use that. For directories:
For files it's nearly identical, you just need another FileFilter implementation with a different implementation of accept.

One final warning: if you call listFiles on a File object that does not represent an existing directory, or if that directory cannot be accessed, listFiles will return null. You need to check that before using the array. To make life easier for you, you can integrate that check in your getDirectories() method:
If listFiles now returns null, you will return an empty array instead.
 
Anders Kviback
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot for putting your time and effort into my programmingproblem. It took some time for me to answer back but now I've managed to solve the whole assignment. I used the array solution for that part.
All the help that you guys give here on this forum is really helpful. Sometimes I just get stuck and then it is really helpful to ask someone here.

Thanks again, Anders
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!