哪里是错误???显示“java.lang.reflect.InvocationTargetException”

时间:2017-04-14 10:22:50

标签: javafx fxml

为什么会发生这种异常,我试图修复它,但看不到任何解决方案 但是,我找不到解决这些异常的线索

// main

package filebrowser;
import javafx.application.Application;
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class FileBrowser extends Application
{

@Override
public void start(Stage stage) throws Exception{
    Parent root=FXMLLoader.load(getClass().getResource("fileExplorer.fxml"));
    Scene scene=new Scene(root);
    stage.setTitle("Windows File Explorer");
    stage.getIcons().add(new 
    Image(ClassLoader.getSystemResourceAsStream("filebrowser/application-x-executable.png")));
    stage.setScene(scene);
    stage.show();
}

public static void main(String[] args){
    launch(args);
}

}

/ *控制器类这是主控制类,我认为错误应该在那里,但还不能弄明白。

package filebrowser;

import java.io.File;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class FXMLcontroller{

@FXML
private ListView<String> listviewFiles;

@FXML
private TreeView<String> treeviewFileBrowse;


public void initialize() {
    assert treeviewFileBrowse != null : "fx:id=\"treeviewFileBrowse\" was not injected: check your FXML file 'FileExplorer.fxml'.";
    TreeItem<String> rootNode=new TreeItem<>("This PC",new ImageView(new Image(ClassLoader.getSystemResourceAsStream("filebrowser/computer.png"))));
    Iterable<Path> rootDirectories=FileSystems.getDefault().getRootDirectories();
    for(Path name:rootDirectories){
        FilePathTreeItem treeNode=new FilePathTreeItem(new File(name.toString()));
        rootNode.getChildren().add(treeNode);
    }
    rootNode.setExpanded(true);
    this.treeviewFileBrowse.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    this.treeviewFileBrowse.setRoot(rootNode);
    //this.listviewFiles.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
}
}  

//文件路径树类

package filebrowser;

import java.io.File;
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.TreeItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class FilePathTreeItem extends TreeItem<String>{
public static Image folderCollapseImage=new Image(ClassLoader.getSystemResourceAsStream("filebrowser/folder.png"));
public static Image folderExpandImage=new Image(ClassLoader.getSystemResourceAsStream("filebrowser/folder-open.png"));
public static Image fileImage=new Image(ClassLoader.getSystemResourceAsStream("filebrowser/text-x-generic.png"));
private boolean isLeaf;
private boolean isFirstTimeChildren=true;
private boolean isFirstTimeLeaf=true;
private final File file;
public File getFile(){return(this.file);}
private final String absolutePath;
public String getAbsolutePath(){return(this.absolutePath);}
private final boolean isDirectory;
public boolean isDirectory(){return(this.isDirectory);}

public FilePathTreeItem(File file){
    super(file.toString());
    this.file=file;
    this.absolutePath=file.getAbsolutePath();
    this.isDirectory=file.isDirectory();
    if(this.isDirectory){
        this.setGraphic(new ImageView(folderCollapseImage));
        //add event handlers
        this.addEventHandler(TreeItem.branchCollapsedEvent(),new EventHandler(){
            @Override
            public void handle(Event e){
                FilePathTreeItem source=(FilePathTreeItem)e.getSource();
                if(!source.isExpanded()){
                    ImageView iv=(ImageView)source.getGraphic();
                    iv.setImage(folderCollapseImage);
                }
            }
        } );
        this.addEventHandler(TreeItem.branchExpandedEvent(),new EventHandler(){
            @Override
            public void handle(Event e){
                FilePathTreeItem source=(FilePathTreeItem)e.getSource();
                if(source.isExpanded()){
                    ImageView iv=(ImageView)source.getGraphic();
                    iv.setImage(folderExpandImage);
                }               
            }
        } );            
}
else{
        this.setGraphic(new ImageView(fileImage));
}
    //set the value (which is what is displayed in the tree)
String fullPath=file.getAbsolutePath();
    if(!fullPath.endsWith(File.separator)){
        String value=file.toString();
        int indexOf=value.lastIndexOf(File.separator);
        if(indexOf>0){
            this.setValue(value.substring(indexOf+1));
        }else{
            this.setValue(value);
        }
    }
}

@Override
public ObservableList<TreeItem<String>> getChildren(){
    if(isFirstTimeChildren){
        isFirstTimeChildren=false;
        super.getChildren().setAll(buildChildren(this));
    }
    return(super.getChildren());
}

@Override
public boolean isLeaf(){
    if(isFirstTimeLeaf){
        isFirstTimeLeaf=false;
        isLeaf=this.file.isFile();
    }
    return(isLeaf);
}

private ObservableList<FilePathTreeItem> buildChildren(FilePathTreeItem treeItem){
    File f=treeItem.getFile();
    if((f!=null)&&(f.isDirectory())){
        File[] files=f.listFiles();
        if (files!=null){
            ObservableList<FilePathTreeItem> children=FXCollections.observableArrayList();
            for(File childFile:files){
                children.add(new FilePathTreeItem(childFile));
            }
            return(children);
        }
    }
    return FXCollections.emptyObservableList();
}

}

// FXML

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.*?><?import javafx.scene.effect.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns="http://javafx.com/javafx/8" 
xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="filebrowser.FXMLcontroller">
<children>
<TreeView fx:id="treeviewFileBrowse" layoutX="2.0" layoutY="78.0" 
prefHeight="304.0" prefWidth="171.0" />
  <Button layoutX="14.0" layoutY="35.0" mnemonicParsing="false" 
prefHeight="22.0" prefWidth="29.0" text="&lt;" />
  <MenuBar layoutY="2.0" prefHeight="25.0" prefWidth="600.0">
    <menus>
      <Menu mnemonicParsing="false" text="File">
        <items>
          <MenuItem mnemonicParsing="false" text="Close" />
        </items>
      </Menu>
      <Menu mnemonicParsing="false" text="Home">
        <items>
          <MenuItem mnemonicParsing="false" text="Delete" />
        </items>
      </Menu>
      <Menu mnemonicParsing="false" text="Share">
        <items>
          <MenuItem mnemonicParsing="false" text="About" />
        </items>
      </Menu>
        <Menu mnemonicParsing="false" text="View">
           <items>
              <MenuItem mnemonicParsing="false" text="Close" />
           </items>
        </Menu>
    </menus>
  </MenuBar>
  <Button layoutX="43.0" layoutY="35.0" mnemonicParsing="false" 
prefHeight="22.0" prefWidth="29.0" text="&gt;" />
  <TextField layoutX="475.0" layoutY="35.0" prefHeight="25.0" 
prefWidth="111.0" promptText="Search quick text" />
  <TableView layoutX="179.0" layoutY="78.0" prefHeight="304.0"  
prefWidth="417.0">
    <columns>
        <TableColumn prefWidth="50.0" text="Icon" />
      <TableColumn prefWidth="144.0" text="Name" />
      <TableColumn minWidth="0.0" prefWidth="106.0" text="Date Modified" />
        <TableColumn minWidth="0.0" prefWidth="69.0" text="Size" />
        <TableColumn minWidth="0.0" prefWidth="45.0" text="Type" />
    </columns>
  </TableView>
  <TextField layoutX="102.0" layoutY="35.0" prefHeight="25.0" 
prefWidth="365.0" promptText="Search Here" />
  <AmbientLight color="CHARTREUSE" lightOn="true" />
  <PointLight color="CHARTREUSE" lightOn="true" />
</children>
</AnchorPane>`

//输出异常

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at filebrowser.FileBrowser.start(FileBrowser.java:14)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Exception running application filebrowser.FileBrowser
Java Result: 1

0 个答案:

没有答案