title: Selecting an object from a ListView in JavaFX
body: <p>I have a ListView that contains "Task "objects. I want to be able to select a task from the ListView and get the values of that specific object. I am trying to do this by calling a method (startTask()) that gets the selected item in the ListView (a task object) and gets its values by using the getter methods in the task object. For the moment i am just printing the object to the console.</p>
  
  <p>When I run the application this is the error that I get.</p>
  
  <pre><code>Exception in Application start method
  java.lang.reflect.InvocationTargetException
  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 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(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
  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(Unknown Source)
  Caused by: javafx.fxml.LoadException: 
  /C:/Users/Brian.Nora-PC/workspace/TaskApp/bin/application/taskapp.fxml:63
  
  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:20)
  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
  Caused by: java.lang.IllegalArgumentException: Unable to coerce #startTask() to interface javafx.event.EventHandler.
  at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:496)
  at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:258)
  at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:54)
  at javafx.fxml.FXMLLoader$Element.applyProperty(FXMLLoader.java:512)
  at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:363)
  at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:325)
  at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:235)
  at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:767)
  at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
  at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
  ... 17 more
  Exception running application application.Main
  </code></pre>
  
  <p>Here is the fxml file that contains the ListView and Button with startTask() method call when the button is clicked </p>
  
  <pre><code>&lt;Tab text="Tasks"&gt;
    &lt;content&gt;
      &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"&gt;
      &lt;children&gt;
       &lt;Label fx:id="noOfTasks" layoutX="403.0" layoutY="294.0" prefHeight="17.0" prefWidth="190.0" text="You have 0 tasks to be completed" /&gt;
                &lt;ListView fx:id="taskList" layoutX="8.0" layoutY="14.0" prefHeight="274.0" prefWidth="585.0" /&gt;
                &lt;Button layoutX="8.0" layoutY="299.0" mnemonicParsing="false" text="Start Task" OnAction="#startTask()"/&gt;
      &lt;/children&gt;
  
      &lt;/AnchorPane&gt;
    &lt;/content&gt;
  &lt;/Tab&gt;
  </code></pre>
  
  <p>Here is the startTask() method</p>
  
  <pre><code>public void startTask(){
      Task taskToStart = taskList.getSelectionModel().getSelectedItem();
      System.out.println(taskToStart);
  
  
  
  }
  </code></pre>
  
  <p>Here is my initialisation of ListView in Controller.java</p>
  
  <pre><code>public ListView&lt;Task&gt; taskList = new ListView&lt;Task&gt;();
  </code></pre>
  
  <p>Any help would be much appreciated thanks.</p>