简体   繁体   中英

TreeView Image throws NullPointerException

This is the code of my Controller class:

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 ()
{



}

This is the code of my function 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;
    }

I don't know why I'm getting the java.lang.NullPointerException . Everything is good, the build path, the path, and when I create the TreeView without picture.

Here is the console output:

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

Where is my mistake? :(

The problem you are having is when you are loading the image, you should use: this.img =new ImageView(new Image( getClass().getClassLoader().getResource("/images/Ford.png").toExternalForm()));

That should solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM