0

So I have my controller where i have the choicebox, i try to use the addall method that takes any collection to populate the choicebox. it works when i hardcode an array as seen but when i try to use the commented out array (the one that calls the data source method), i get this error:

     [echo] Executing /Users/alexanderokonkwo/Desktop/Projects/MaintenanceApp/dist/run797087945/MaintenanceApp.jar using platform /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/bin/java
     [java] Connection successful
     [java] Exception in Application start method
     [java] java.lang.reflect.InvocationTargetException
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
     [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     [java]     at java.lang.reflect.Method.invoke(Method.java:498)
     [java]     at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
     [java]     at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
     [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     [java]     at java.lang.reflect.Method.invoke(Method.java:498)
     [java]     at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
     [java] Caused by: java.lang.RuntimeException: Exception in Application start method
     [java]     at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
     [java]     at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
     [java]     at java.lang.Thread.run(Thread.java:748)
     [java] Caused by: javafx.fxml.LoadException: 
     [java] file:/Users/alexanderokonkwo/Desktop/Projects/MaintenanceApp/dist/run797087945/MaintenanceApp.jar!/maintenanceapp/FXMLDocument.fxml:12
     [java] 
     [java]     at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
     [java]     at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
     [java]     at maintenanceapp.MaintenanceApp.start(MaintenanceApp.java:24)
     [java]     at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
     [java]     at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
     [java]     at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
     [java]     at java.security.AccessController.doPrivileged(Native Method)
     [java]     at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
     [java]     at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
     [java] Caused by: java.lang.NullPointerException
     [java]     at maintenanceapp.model.Datasource.getMaintenanceTypesAsArray(Datasource.java:57)
     [java]     at maintenanceapp.FXMLDocumentController.<init>(FXMLDocumentController.java:29)
     [java]     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     [java]     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
     [java]     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
     [java]     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
     [java]     at java.lang.Class.newInstance(Class.java:442)
     [java]     at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
     [java]     at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
     [java]     at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
     [java]     at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
     [java]     at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
     [java]     at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
     [java]     ... 14 more
     [java] Exception running application maintenanceapp.MaintenanceApp

this is my controller class:

public class FXMLDocumentController implements Initializable {
    Datasource datasource = new Datasource();
    
    @FXML
    private ChoiceBox<String> typeChoiceBox;
    
    private String typeArray[] = {"Oil", "Spark Plugs","Transmission Fluid"};
    
//    private String typeArray[] = datasource.getMaintenanceTypesAsArray();
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
        typeChoiceBox.getItems().addAll(typeArray);
        
    }    
    
}

and this is the method in my datasource class that returns an array with the information I want to display. I already printed out the array and its populating correctly. For some reason i just cant get it to show up in the choicebox. When i hardcode the array it works but when i use the other array thats commented out (the one that calls the datasource method), i get the error. Any one have a clue?

    public String[] getMaintenanceTypesAsArray() {

        try (Statement statement = conn.createStatement();
             ResultSet results = statement.executeQuery("SELECT * FROM " + TABLE_MAINTENANCETYPE)){

            
            List<MaintenanceType> types = new ArrayList<>();
            while(results.next()){
                MaintenanceType type = new MaintenanceType();
                type.setType(results.getString(COLUMN_TYPE));
                types.add(type);
                
            }
            
            String typesArray[];
            typesArray = new String[types.size()];
            for (int i = 0; i < types.size(); i++){
                typesArray[i] = types.get(i).getType();
            }            
            
        return typesArray;
        } catch (SQLException e) {
            System.out.println("Query failed: " + e.getMessage());
            return null;
        } 
    }
owwix
  • 73
  • 5
  • 1
    Which line in `Datasource` is line 57? – James_D Aug 08 '22 at 18:52
  • @James_D It's the first line of the try block in my getMaintenanceTypesAsArray() method – owwix Aug 08 '22 at 18:56
  • 1
    You mean this one: `conn.createStatement()`? If so, the database connection is null. – jewelsea Aug 08 '22 at 19:05
  • 1
    See [how to connect JavaFX with sqllite](https://edencoding.com/connect-javafx-with-sqlite/). You might use a different database system, but the principles for JDBC connections are similar. – jewelsea Aug 08 '22 at 19:10

0 Answers0