aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JFileChooser selecting multiple files from different directories Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFileChooser selecting multiple files from different directories" Watch "JFileChooser selecting multiple files from different directories" New topic
Author

JFileChooser selecting multiple files from different directories

Matthew Busse
Ranch Hand

Joined: Sep 29, 2010
Posts: 52
Hello Java Ranchers,

I'm trying to use a JFileChooser to select multiple files, either from the same directory or multiple directories. I have a fundamental misunderstanding of how to do this.

To select multiple files from the same directory, all I need to do is enable MultiSelection, which I've done. That works fine.

To select multiple files from different directories, I'm thinking I need to choose the files individually, then add them to an array of files, then pass that array to my other classes. This is where I'm running into problems, I think it has something to do with the File[] returned by the .getSelectedFiles method of the JFileChooser. I don't know what File[] is, the javadoc says it's "An abstract representation of file and directory pathnames." I'm not sure what to do with that.

This is what I tried first:


This gives me a run-time Null pointer exception, pointing to line 13. I can print out the pathname of the file, so it seems like there is a file there, but when I try to add that file to the array, it gives me the null pointer exception. I tried creating a new File from the File[], but that didn't work either. So now I'm stuck.

Does anyone know how to make this work, or am I entirely on the wrong track for selecting multiple files from different directories?

Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

That just means that the "filesToMerge" variable is null. You're getting distracted by the JFileChooser, which is working perfectly well. And so is your code, up to that point.
Matthew Busse
Ranch Hand

Joined: Sep 29, 2010
Posts: 52
Paul Clapham wrote:That just means that the "filesToMerge" variable is null. You're getting distracted by the JFileChooser, which is working perfectly well. And so is your code, up to that point.


Great, thanks! I thought the sourceFile variable was null.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JFileChooser selecting multiple files from different directories