0

The program prompts the user for selecting 1 or more files using FileChooser.
I save that to a list and then I send that list to this method down here. Then right after selecting the files and pressing save, I get a NullPointerException in the stack trace.
The program still keeps runs afterwards and it does copy the files successfully.
The problem is, what on earth is pointing to null here??? I have less than a year of experience in java.

public class PhotoManager {

    private final FileChooser fileChooser = new FileChooser();
    private final String pathname = "C:\\JavaAlbumApp";
    private final File dir = new File(pathname);
    //private final Desktop desktop = Desktop.getDesktop();
    protected List<File> photos;

    PhotoManager()
    {


        // If the default directory JavaAlbumApp doesn't exist then create it.
        boolean exists = dir.exists();
        try
        {
            if(!exists)dir.mkdir();    
        }
        catch(SecurityException se)
        {
            System.out.println("Could not create directory");
        }

    }

protected void importFile(Stage primaryStage) 
{ 
        // Set extension filters
        setExtFilters();
        // Set title
        fileChooser.setTitle("Select Picture/s");
        // Prompt user to select the photo/s they want to copy/save
        photos = fileChooser.showOpenMultipleDialog(primaryStage);
        copyFileUsingJava7Files();

    System.out.println(fileChooser.getTitle()); // Select Picture/s
    System.out.println(fileChooser.toString()); // javafx.stage.FileChooser@229f9227
    System.out.println(fileChooser.getInitialFileName()); // null
    System.out.println(fileChooser.getInitialDirectory().getName()); //JavaAlbumApp
    System.out.println(fileChooser.getInitialDirectory().toString()); //C:\JavaAlbumApp
    System.out.println(fileChooser.getInitialDirectory().getPath()); //C:\JavaAlbumApp
    System.out.println(fileChooser.getInitialDirectory().getAbsolutePath()); //C:\JavaAlbumApp
    System.out.println(fileChooser.getInitialDirectory().getParent()); //C:\
}

 private void copyFileUsingJava7Files()
 {   // Step 1: 
     // Copy list of photos to dir variable "C:\\JavaAlbumApp".  
    for(File photo : photos)
    {
        if(photo != null)
        {
            try{
                Files.copy(photo.toPath(), dir.toPath().resolve(photo.getName()));
               //this.desktop.open(photo);
            } catch(IOException e){
               e.printStackTrace();
            }
        }
    }
 }
}

heres the stack trace

ant -f C:\\Users\\edwin\\OneDrive\\Documents\\NetBeansProjects\\PhotoAlbumApp jfxsa-run
init:
Deleting: C:\Users\edwin\OneDrive\Documents\NetBeansProjects\PhotoAlbumApp\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\edwin\OneDrive\Documents\NetBeansProjects\PhotoAlbumApp\build\built-jar.properties
compile:
Detected JavaFX Ant API version 1.3
jfx-deployment:
jar:
Copying 12 files to C:\Users\edwin\OneDrive\Documents\NetBeansProjects\PhotoAlbumApp\dist\run365021180
jfx-project-run:
Executing C:\Users\edwin\OneDrive\Documents\NetBeansProjects\PhotoAlbumApp\dist\run365021180\PhotoAlbumApp.jar using platform C:\Program Files\Java\jdk1.8.0_121\jre/bin/java
Select Picture/s
null
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at photoalbumapp.PhotoManager.importFile(PhotoManager.java:63)
    at photoalbumapp.PhotoAlbumApp.lambda$start$0(PhotoAlbumApp.java:122)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Deleting directory C:\Users\edwin\OneDrive\Documents\NetBeansProjects\PhotoAlbumApp\dist\run365021180
jfxsa-run:
BUILD SUCCESSFUL (total time: 18 seconds)
redPill
  • 13
  • 4

1 Answers1

0

I believe your problem is this line:

photos = fileChooser.showOpenMultipleDialog(primaryStage);

The Javadoc for FileChooser#showOpenMultipleDialog says that it returns null when no file is selected.

https://docs.oracle.com/javase/8/javafx/api/javafx/stage/FileChooser.html#showOpenMultipleDialog-javafx.stage.Window-

Then the null pointer occurs at for(File photo : photos) when copyFileUsingJava7Files() tries to iterate over a collection of photos that is set to null.

Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
  • So to fix it I have to pre-set photos to something? I dont understand because the program successfully stores images to the List photos. I know it does because I check my directory and all the photos were successfully copied. – redPill Mar 08 '17 at 08:08
  • Ideally, you should try to understand why FileChooser doesn't know which file is selected upon save. – Luciano van der Veekens Mar 08 '17 at 08:10
  • ok thanks I will research into it. – redPill Mar 08 '17 at 08:22