Directory folder - null pointer exception on MAC only
import java.io.File;
import com.cycling74.jitter.JitterMatrix;
import com.cycling74.jitter.JitterObject;
public class MoviesManager extends Thread {
/**
* Constant - Path to the folder which contains the video
*/
private static final String PATH_FOLDER = "afp://bam.idmi.poly.edu/Users/carolinebouchat/Documents/videos";
private int nbMovies;
private File directory;
private JitterMatrix jmOut;
public MoviesManager(JitterMatrix jmOut) {
directory = new File(PATH_FOLDER);
this.jmOut = jmOut;
}
/**
* Method - Free peers
*
* @param m
* movies
*/
private void freePeers(MaxVideo[] m) {
if (m != null)
for (int i = m.length-1; i >= 0; i++) {
m[i].getMovie().freePeer();
m[i].destroy();
}
}
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
super.run();
File[] files;
//while (true) {
files = directory.listFiles();
nbMovies = files.length;
// If the videos' array is not created
if (MaxTest.videos == null) {
MaxTest.videos = new MaxVideo[nbMovies];
}
else {
// Free the object
freePeers(MaxTest.videos);
// Reinitialize the array;
MaxTest.videos = new MaxVideo[nbMovies];
}
for (int i = 0; i < nbMovies; i++) {
// Create the video object
MaxTest.videos[i] = new MaxVideo(new JitterObject("jit.qt.movie"), this.jmOut);
System.out.println("file : "+files[i].getName());
MaxTest.videos[i].getMovie()
.send(
"read",
"afp://bam.idmi.poly.edu/Users/carolinebouchat/Documents/videos/"+files[i].getName());
MaxTest.videos[i].getMovie().send("autostart", 0);
MaxTest.videos[i].start();
}
// Wait 5 seconds before check the folder a new time
//}
}
}
I have a java null pointer exception on the line "files = directory.listFiles();" : so I find that come fron "nbMovies = files.length;" but I don't know why it is not working.
The path is right !
The same class work perfectly on windows !
Thanks for help me.
I stripped it down to some simple code that I ran outside of Max:
import java.io.File;
public class AfpFileTest {
public static void main(String args[]) {
File directory = new File("afp://bam.idmi.poly.edu/Users/carolinebouchat/Documents/videos");
File[] files = directory.listFiles();
System.out.println("Found " + files.length + " files");
}
}
I can't get it to work on either OS X or Windows. NullPointerException in both cases. directory.listFiles() is returning null. If you check the javadocs: http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#listFiles()
it says: "Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs."
I'm fairly certain this is because you are using the afp:// protocol. I'm surprised it works for you on Windows, maybe you installed some sort of special library to support it? I wouldn't expect Apple's proprietary remote network file system to work out-of-the-box with Java's standard File API...
I'd suggest you find an alternative way to access the files. I am not familiar with afp so I don't have any specific suggestions. But maybe there is a way to mount the remote network server so that it looks like a normal local hard drive to Java. Otherwise, I'd see if it's possible to use http or ftp instead. Those are much more common open protocols and should be easier to get working with Java.
I tried with a local path on both :
- windows : worked (local path)
- mac : never worked (local and server path)
To get it work on windows, you have to replace the path at 2 places : when I create the directory object and when I am looking for all files in the directory.
here :
File directory = new File(_PATH_);
and here :
MaxTest.videos[i].getMovie().send("read", _PATH_+files[i].getName());
carob wrote on Wed, 15 April 2009 00:06I tried with a local path on both :
- windows : worked (local path)
- mac : never worked (local and server path)
That's weird. Local paths should be ok. What are you using for the local path?
carob wrote on Wed, 15 April 2009 08:06
MaxTest.videos[i].getMovie().send("read", _PATH_+files[i].getName());
Well, you don't really want to do this... look at the File constructor with parent and child arguments:
<http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#File(java.io.File,%20java.lang.String)>
It may not solve your problem but it is a lot neater.
Btw, the forum software is broken, so you'll have to select the entire URL manually.
Local path work but only for windows not for mac
carob wrote on Wed, 15 April 2009 11:27Local path work but only for windows not for mac
My guess is you are not forming the path correctly on Mac. Try calling file.exists() after creating the File object and make sure it's true...
Sorry if I'm stating the obvious, but you realize paths will look different on Windows and Mac? So something like "C:Program FilesMax5" on Windows is going to be something like "/Applications/Max5" on Mac.
Since you probably need to use different paths you can do something like:
if(System.getProperty("os.name").contains("Windows")) {
// set windows path
} else {
// set mac path
}
It's also possible to find files relative to some known file in the Max installation (like your patcher file if it's on the Max search path). This might be a better solution. I can provide more details if needed.
My path was wrong : I have to use the local one and no the server one.
Thank you for your help.