将文件复制到另一个文件夹会抛出java.lang.NullPointerException

时间:2017-03-08 07:13:16

标签: java javafx file-io nullpointerexception

程序提示用户使用FileChooser选择1个或多个文件。
我将其保存到列表中,然后我将该列表发送到此方法。然后在选择文件并按下保存后,我在堆栈跟踪中得到一个NullPointerException。
程序后来仍然保持运行并且成功复制文件。
问题是,究竟是什么指向在这里为空???我有不到一年的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();
            }
        }
    }
 }
}

继承堆栈跟踪

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)

1 个答案:

答案 0 :(得分:0)

我相信你的问题就在这一行:

photos = fileChooser.showOpenMultipleDialog(primaryStage);

FileChooser的Javadoc#showOpenMultipleDialog说它在没有选择文件时返回null。

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

copyFileUsingJava7Files()尝试迭代设置为null的照片集合时,空指针出现在for(File photo : photos)