TreeView Image抛出NullPointerException

时间:2017-01-29 18:09:47

标签: image javafx nullpointerexception imageview treeview

这是我的Controller类的代码:

public class Controller implements javafx.fxml.Initializable{

@FXML private Button btn_home ;
@FXML private Button btn1;
@FXML private VBox vbox_list;
@FXML public TreeView tree_view;
@FXML public Label lbl;
public ImageView img;



@Override
public void initialize(URL arg0, ResourceBundle arg1) {

    this.img =new ImageView(new Image( getClass().getResource("/images/Ford.png").toExternalForm()));
    TreeViewHelper helper = new TreeViewHelper();
    ArrayList<TreeItem<String>> products = helper.getmakes(img);
    tree_view.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
    TreeItem<String> rootItem = new TreeItem<String>("Marques");
    rootItem.getChildren().addAll(products);
    tree_view.setRoot(rootItem);
    tree_view.setShowRoot(false);
    tree_view.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem>() {

        @Override
        public void changed(ObservableValue<? extends TreeItem> arg0, TreeItem arg1, TreeItem arg2) {
            // TODO Auto-generated method stub
            System.out.println("new: " + arg2.getValue());
            TreeItem<String> selectedItem = (TreeItem<String>) arg2;
            String st = arg2.getValue().toString();
                        if ( st.equals("Alfa Romeo"))
                        st = "alfa%20romeo";
                        else if ( st.equals("Mercedes-Benz"))
                        st = "mercedes%20benz";
                        else if ( st.equals("Rolls-Royce"))
                        st = "Rolls%20Royce";
                        else if ( st.equals("Aston Martin"))
                        st = "aston%20martin";

            System.out.println(st);
                TreeViewHelper helper = new TreeViewHelper();
                ArrayList<TreeItem<String>> products = helper.getmodels(st);
                selectedItem.getChildren().addAll(products);
                selectedItem.setExpanded(true);
                tree_view.requestFocus();
        }
        });
    //System.out.print(t.get_list_makes().size());

}

@FXML public void mouse_clicked ()
{



}

这是我的函数get_makes:

的代码
    public ArrayList<TreeItem<String>> getmakes(ImageView m)
    {

    All_makes all_makes = (All_makes) new parse_object <All_makes> (All_makes.class).ParseUri("https://api.edmunds.com/api/vehicle/v2/makes?state=new&fmt=json&api_key=9t82jrgr8c3bv6vusw8j75fu");
    ArrayList<TreeItem<String>> makes = new ArrayList<TreeItem<String>>();
    for ( int i = 0 ; i< all_makes.get_list_makes().size(); i++)
    {

        TreeItem<String> make = new TreeItem<String>(all_makes.get_list_makes().get(i).get_mk_name(),m);
        make.setGraphic(m);
        makes.add(make);

    }
    return makes;
    }

我不知道为什么我得到java.lang.NullPointerException。 一切都很好,构建路径,路径,以及我创建TreeView时没有图片。

这是控制台输出:

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
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 application.Main.start(Main.java:35)
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)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
  at Controller.Controller.initialize(Controller.java:56)
  at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
   ... 17 more

我的错误在哪里? :(

1 个答案:

答案 0 :(得分:0)

您遇到的问题是在加载图片时,您应该使用:this.img =new ImageView(new Image( getClass().getClassLoader().getResource("/images/Ford.png").toExternalForm()));

这应该可以解决你的问题。