0

This is what I have:

  • FXML: FTPView.fxml

  • Controller: FTPController.java

The FTPView.fxml contains a TreeView. I want to add TreeItem'sto that TreeView programmatically but I am not able to do it.

This is the way I am doing it:

  1. Implementing the Initializable interface in the controller.
  2. Adding the following code to the initialize() method, setting the root TreeItem to the TreeView.

    @Override
        public void initialize(URL location, ResourceBundle resources) {
    
            rootItem = new TreeItem<>();
            tv_serverfiles.setRoot(rootItem);
        }
    
  3. Other stuff filling the TreeView.

At one point i was doing it completely wrong, setting the rootelement on the constructor. Then, reading some post in SO, i noticed that i should implement Initializable interface, so i would get the method initialize that would set the root element to my TreeView until all FXML stuff had loaded.

But i am still getting the same error, the NullPointerException.

CODE

FTPView.fxml

I think should skip this one because the unique interesting line is <TreeView fx:id="tv_serverfiles"/>

FTPController.java

public class FTPController implements Initializable{

private FTPClient ftpClient;
private TreeItem<String> rootItem;

private final String 
            CONNECTED="Conectado", 
            DISCONNECTED="Desconectado", 
            ERROR="Error de conexión";

@FXML
private Pane pnMain;

@FXML
private TextField tf_ipaddress;

@FXML
private TextField tf_port;

@FXML
private TextField tf_user;

@FXML
private PasswordField pf_password;

@FXML
private Button btn_connect;

@FXML
private Button btn_sendfile;

@FXML
private TextField tf_filepath;

@FXML
private Button btn_filechooser;

@FXML
private AnchorPane sp_record;

@FXML
private Button btn_requestfile;

@FXML
private ProgressBar pb_progresstransfer;

@FXML
private Label tf_status;

@FXML
private TitledPane tp_transfer_status;

@FXML
private Pane pnRemoteExplorer;

@FXML
private TreeView<String> tv_serverfiles;

@FXML
private Button btn_refresh;

@FXML
private Button btn_goback;

@Override
public void initialize(URL location, ResourceBundle resources) {
    rootItem = new TreeItem<>();

    tv_serverfiles.setRoot(rootItem);
}


/**
 * Carga los ficheros remotos en el TreeTableView para poder listarlos correctamente
 * @param files
 */
private void loadRemoteDirectory(FTPFile[] ftpFiles) {

    //Evento que actualiza los archivos en la tabla
    tv_serverfiles.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<String>>() {

        @Override
        public void changed(ObservableValue<? extends TreeItem<String>> observable, TreeItem<String> oldValue,
                TreeItem<String> newValue) {

            rootItem.getChildren().clear();

            try {
                ftpClient.changeWorkingDirectory(newValue.getValue());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            for(FTPFile f: ftpFiles) {
                TreeItem<String> element = new TreeItem<String>(f.getName());
                rootItem.getChildren().add(element);
            }

        }
    });
}

}

ERROR

javafx.fxml.LoadException: 
/D:/DAM2/Projects/EclipseOxygen/ClientFTPSMTP/bin/FXML/FTPView.fxml

    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 interfaz.controller.MainController.openFTPUI(MainController.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    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.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.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$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    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(Unknown Source)
Caused by: java.lang.NullPointerException
    at interfaz.controller.FTPController.initialize(FTPController.java:103)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 49 more
Gholamali Irani
  • 4,391
  • 6
  • 28
  • 59
JD0001
  • 131
  • 3
  • 11
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – SedJ601 Dec 22 '17 at 21:44
  • Create a [MCVE]. Are you loading `FTPView.fxml` in `openFTPUI()` in the `MainController` class? – James_D Dec 22 '17 at 22:39
  • Yes, you are right – JD0001 Dec 22 '17 at 22:58
  • And line 103 is `tv_serverfiles.setRoot(...)`, and you're certain the FXML you copied is exactly as it is in your FXML file? If so, nothing appears wrong; maybe try cleaning and rebuilding the project (in particular, make sure the latest version of the FXML is copied to the build folder). – James_D Dec 22 '17 at 23:05

0 Answers0