title: Java Error when using the line usbList.add(roots[i]);
body: <p>Here is my Main:</p>
  
  <pre><code>package sample;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  
  public class Main extends Application {
  
  public static void main(String[] args) {
  
      launch(args);
  }
  
  @Override
  public void start(Stage primaryStage) throws Exception{
      Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
      primaryStage.setTitle("USB Configuration");
      primaryStage.setScene(new Scene(root, 300, 275));
      primaryStage.show();
  
  }
  
  
  
  }
  </code></pre>
  
  <p>Here is my controller:</p>
  
  <pre><code>package sample;
  
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.fxml.Initializable;
  import javafx.scene.control.ComboBox;
  
  import java.io.File;
  import java.net.URL;
  import java.util.ResourceBundle;
  
  public class Controller implements Initializable{
  public ComboBox usbListComboBox;
  public ObservableList usbList;
  public File[] roots = File.listRoots();
  public void initialize(URL location, ResourceBundle resources){
          for (int i = 0; i&lt; roots.length; i++){
             usbList.add(roots[i]);
          }
          usbListComboBox.setItems(usbList);
  }
  }
  </code></pre>
  
  <p>Here is my fxml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  
  &lt;?import javafx.scene.control.cell.PropertyValueFactory?&gt;
  &lt;?import javafx.collections.FXCollections?&gt;
  &lt;GridPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" 
  xmlns:fx="http://javafx.com/fxml"
        fx:controller="sample.Controller"&gt;
  &lt;children&gt;
    &lt;ComboBox fx:id = "usbListComboBox" promptText="Configuration"&gt;
       &lt;items&gt;
  
       &lt;/items&gt;
    &lt;/ComboBox&gt;
  &lt;/children&gt;
  
  &lt;/GridPane&gt;
  </code></pre>
  
  <p>The error I'm getting:</p>
  
  <pre><code>Exception in Application start method
  java.lang.reflect.InvocationTargetException
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:566)
      at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
      at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:566)
      at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
  Caused by: java.lang.RuntimeException: Exception in Application start method
      at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
      at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
      at java.base/java.lang.Thread.run(Thread.java:834)
  Caused by: javafx.fxml.LoadException: 
  /C:/Users/JohnsonL/Desktop/flashTranserProj/flashTransferProj/out/production/flashTransferProj/sample/sample.fxml
  
      at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
      at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
      at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
      at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)
      at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
      at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
      at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
      at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)
      at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)
      at sample.Main.start(Main.java:18)
      at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
      at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
      at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
      at java.base/java.security.AccessController.doPrivileged(Native Method)
      at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
      at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
      at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
      at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
      ... 1 more
  Caused by: java.lang.NullPointerException
      at sample.Controller.initialize(Controller.java:18)
      at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
      ... 17 more
  Exception running application sample.Main
  </code></pre>
  
  <p>The error I am getting is coming from the line: </p>
  
  <pre><code>usbList.add(roots[i]);
  </code></pre>
  
  <p>When I remove this line It runs fine. I have seen other posts with similar errors and have had this error in the past but nothing that I have seen has worked. I am unsure if I am doing something wrong when I am trying the solutions so they might solve my issue and I am doing something wrong.</p>