JAVAFX - 初学者的错误?

时间:2015-03-02 11:06:06

标签: javafx treeview

可能这只是一个初学者的错误,但每当我尝试创建一个新的FXML项目并将此示例代码添加到main.java时(示例由Oracle自己编写以显示示例,因此代码可以没有错)我得到了异常错误。

BUT!如果我删除这一行:

 private final Node rootIcon =  new ImageView(new Image(getClass().getResourceAsStream("root.png")));

而且我没有给根打电话,代码运行良好! (但以防万一.java文件旁边有一个root.png)

可能是什么原因?我确信甲骨文没有编写无效的代码。

代码:

import java.util.Arrays;     import java.util.List;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Test extends Application {
    List<Employee> employees = Arrays.<Employee>asList(
            new Employee("a1", "A"),
            new Employee("a2", "A"),
            new Employee("a3", "A"),
            new Employee("b1", "B"),
            new Employee("b2", "B"),
            new Employee("b3", "B"),
            new Employee("c1", "C"),
            new Employee("c2", "C"),
            new Employee("c3", "C"),
            new Employee("c4", "C"),
            new Employee("e1", "E"));
    private final Node rootIcon =  new ImageView(new Image(getClass().getResourceAsStream("root.png")));
    TreeItem<String> rootNode = new TreeItem<String>("Root",rootIcon);

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

    @Override
    public void start(Stage stage) {
        rootNode.setExpanded(true);
        for (Employee employee : employees) {
            TreeItem<String> empLeaf = new TreeItem<String>(employee.getName());
            boolean found = false;
            for (TreeItem<String> depNode : rootNode.getChildren()) {
                if (depNode.getValue().contentEquals(employee.getDepartment())){
                    depNode.getChildren().add(empLeaf);
                    found = true;
                    break;
                }
            }
            if (!found) {
                TreeItem depNode = new TreeItem(employee.getDepartment());
                rootNode.getChildren().add(depNode);
                depNode.getChildren().add(empLeaf);
            }
        }
        stage.setTitle("Tree View Sample");
        VBox box = new VBox();
        final Scene scene = new Scene(box, 400, 300);
        scene.setFill(Color.LIGHTGRAY);

        TreeView<String> treeView = new TreeView<String>(rootNode);
        treeView.setShowRoot(true);
        treeView.setEditable(true);
        box.getChildren().add(treeView);
        stage.setScene(scene);
        stage.show();
    }
    public static class Employee {

        private final SimpleStringProperty name;
        private final SimpleStringProperty department;

        private Employee(String name, String department) {
            this.name = new SimpleStringProperty(name);
            this.department = new SimpleStringProperty(department);
        }

        public String getName() {
            return name.get();
        }

        public void setName(String fName) {
            name.set(fName);
        }

        public String getDepartment() {
            return department.get();
        }

        public void setDepartment(String fName) {
            department.set(fName);
        }
    }
}

异常错误:

Exception in Application constructor
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at com.javafx.main.Main.launchApp(Main.java:698)
        at com.javafx.main.Main.main(Main.java:871)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class Test
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:393)
        at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
        at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
        at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:276)
        ... 3 more
Caused by: java.lang.NullPointerException: Input stream must not be null
        at javafx.scene.image.Image.validateInputStream(Image.java:1001)
        at javafx.scene.image.Image.<init>(Image.java:624)
        at Test.<init>(Test.java:29)
        ... 8 more
Java Result: 1

1 个答案:

答案 0 :(得分:2)

加载图片时缺少包名称。只需使用以下内容:

new ImageView(new Image(getClass().getResourceAsStream("/packagename/root.png")));

只有当您的图像位于类路径中的某个子目录内时,才需要这样做。