-1

I coded a button in my project to trigger a method that generates multiple anchor panes on a parent anchor pane and displays database information on the label nodes in each of the generated anchor panes. This button works sometimes and other times This error triggers seemingly at random. This is the error:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch

It points to no specific line in my application at all just internal classes in java. I have checked my code and cannot find any methods that aren't getting the right argument data types related to this code.

 public void generateMultiAnchorPanes(AnchorPane pane) {
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        scrollPane.setFitToHeight(true);
        
        VBox vbHolder = new VBox();
        vbHolder.setFillWidth(true);
       
        
        List<Integer> listIDs = new  ArrayList<>();
        listIDs = StaffData.retrieveAllEmployeeIDs();
        System.out.println("the list of IDs" + listIDs);
        double topOffset = 0.0;
        for (int i : listIDs) {
            AnchorPane innerAnchorPane = createAnchorPane(i);
            innerAnchorPane.setPadding(new Insets(5)); // Add padding around the AnchorPane
            AnchorPane.setTopAnchor(innerAnchorPane, topOffset);
            AnchorPane.setLeftAnchor(innerAnchorPane, 10.0);
            innerAnchorPane.setStyle("-fx-background-color: #FFFFFF;");
            topOffset += 180.0; // Increase the offset for the next row
            
            vbHolder.getChildren().add(innerAnchorPane);
            
        }
      scrollPane.setContent(vbHolder);

    AnchorPane.setTopAnchor(scrollPane, 0.0);
    AnchorPane.setLeftAnchor(scrollPane, 0.0);
    AnchorPane.setRightAnchor(scrollPane, 0.0);
    AnchorPane.setBottomAnchor(scrollPane, 0.0);

    pane.getChildren().add(scrollPane);
    }

This code is for making the anchor panes. and this is the createAnchorPane method code:

  private AnchorPane createAnchorPane(int EmployeeID){
        
    AnchorPane innerAnchorPane = new AnchorPane();
    
    VBox outerVBox = createVBox(EmployeeID); 
    
     
    innerAnchorPane.getChildren().addAll(outerVBox);
    
   return innerAnchorPane;          
}

This then is code for the outerVBox creation

  private VBox createVBox(int EmployeeID) {
   
    String[] names = StaffData.retrieveName(EmployeeID);
    boolean clockedIn = StaffData.getClockedIn(EmployeeID);
    boolean transIsActive = StaffData.isTransActive(EmployeeID);
    double globalStaffAverageTrans[] = StaffData.getLIfetimeAverageTransTime(EmployeeID);
    String formattedAverageInMins = String.format("%.0f", globalStaffAverageTrans[0]);
    double LiveTransAverage[] = StaffData.getLiveAverageTransTime(EmployeeID);
    String formattedAverageInDeconds = String.format("%.0f", LiveTransAverage[0]);
    String strTimeInCompany = StaffData.getYearsMonthsDaysWorkingString(EmployeeID);
    String strTotalAmountOfTransactions = StaffData.StaffTransactionTotal(EmployeeID);
    
    Font font = Font.font("Arial", 15);
    
    
    Font fontSubHeading = Font.font("Calisto MT", 15);
        
    VBox outerVBox = new VBox();
    
     Image image = new Image("/Images/EmployeeIcon.jpg");

        // Calculate the maximum dimensions to fit the image within the screen
        Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
        double maxWidth = screenBounds.getWidth() * 0.15; // 80% of screen width
        double maxHeight = screenBounds.getHeight() * 0.15; // 80% of screen height

        // Create an ImageView and set the image with proportional resizing
        ImageView imageView = new ImageView(image);
        imageView.setPreserveRatio(true);
        imageView.setFitWidth(maxWidth);
        imageView.setFitHeight(maxHeight);

        HBox employeeTitle = new HBox();
        employeeTitle.setSpacing(20);
        employeeTitle.setAlignment(javafx.geometry.Pos.CENTER);
        employeeTitle.setPrefWidth(884);
        employeeTitle.setPrefHeight(50);
        employeeTitle.setStyle("-fx-background-color: #00A2D3;");


        Label lblEmployeeName = new Label(names[0]+ " " + names[1]);
        lblEmployeeName.setFont(font);
        Label lblEmployeeIDDisplay = new Label("Employee ID: " + String.valueOf(EmployeeID));
        lblEmployeeIDDisplay.setFont(font);

        employeeTitle.getChildren().addAll(imageView, lblEmployeeName, lblEmployeeIDDisplay);


        VBox alignmentBox = new VBox();
        
        Label lblClockedIn = new Label();
        Label lblIsTramsActive = new Label();
        Label lblAmountOfTranactionToday = new Label();
        Label lblAverageTransactionTime = new Label();

        
            if (clockedIn && transIsActive){
             strClockedIn = "Clocked In";
             lblClockedIn.setTextFill(Color.GREEN);
             strTransIsActive = "Transaction Active";
             lblIsTramsActive.setTextFill(Color.GREEN);
        }
        else if (clockedIn == true && transIsActive == false){
            strClockedIn =  "Clocked In";
            lblClockedIn.setTextFill(Color.GREEN);
            strTransIsActive = "Transaction Not Active";
            lblIsTramsActive.setTextFill(Color.RED);
        }
        else {
            strClockedIn =  "Not Clocked In";
            lblClockedIn.setTextFill(Color.RED);
            strTransIsActive = "Transaction Not Active";
            lblIsTramsActive.setTextFill(Color.RED);
        }
            lblClockedIn.setText(strClockedIn);
            lblIsTramsActive.setText(strTransIsActive);

        if (LiveTransAverage[0] > globalStaffAverageTrans[0]){
            lblAverageTransactionTime.setTextFill(Color.RED);
        }
        else if (LiveTransAverage[0] == globalStaffAverageTrans[1] && LiveTransAverage[1] > globalStaffAverageTrans[1]){
            lblAverageTransactionTime.setTextFill(Color.RED);
        }
        else {
            lblAverageTransactionTime.setTextFill(Color.GREEN);
        }
        
        lblAmountOfTranactionToday.setText("Transactions Today: " + StaffData.getTodaysTransactionsStaff(EmployeeID));
        lblAverageTransactionTime.setText("Live Average Transaction Time: " + formattedAverageInDeconds + "mins " + LiveTransAverage[1] + "secs");
        
        HBox employeeLiveData = new HBox();
        employeeLiveData.setSpacing(20);
        employeeLiveData.setStyle("-fx-background-color: #FF9900;");
        employeeLiveData.setPrefHeight(30);
        employeeLiveData.setPrefWidth(895);

        employeeLiveData.getChildren().addAll(lblClockedIn, lblIsTramsActive, lblAmountOfTranactionToday,lblAverageTransactionTime);

        HBox HBOrganiser = new HBox();
        HBOrganiser.setSpacing(50);


        Label lblUsefulData = new Label("Useful Statistics");
        lblUsefulData.setFont(fontSubHeading);

            GridPane usefulData = new GridPane();
            usefulData.setHgap(20); // Horizontal gap between cells
            usefulData.setVgap(20); // Vertical gap between cells
            usefulData.setStyle("-fx-background-color: #FFFFFF;");

            Label lengthOfTimeWorking = new Label();
            Label lblLifeTimeAverageTransTime = new Label();
            Label lblTotalAmountTransactions = new Label();
            Label lblTotalPaymentFromStart = new Label();
            Label lblGlobalStaffTransAverage = new Label();
            Label lblSickDaysTaken = new Label("Cell (2, 1)");

         
           lengthOfTimeWorking.setText("Time in Company: " + strTimeInCompany);
           lblTotalAmountTransactions.setText("Total Transactions: " + strTotalAmountOfTransactions);
           lblGlobalStaffTransAverage.setText("Lifetime Averaage Transaction Time: " + formattedAverageInMins + "mins " + globalStaffAverageTrans[1] + "sec");


            

            // Adding labels to the GridPane at specific positions
            usefulData.add(lengthOfTimeWorking, 0, 0);
            usefulData.add(lblLifeTimeAverageTransTime, 0, 1);
            usefulData.add(lblTotalAmountTransactions, 1, 0);
            usefulData.add(lblTotalPaymentFromStart, 1, 1);
            usefulData.add(lblGlobalStaffTransAverage, 2, 0);
            usefulData.add(lblSickDaysTaken, 2, 1);


        VBox.setMargin(employeeLiveData, new Insets(0, 0, 10, 10)); // Add some left margin
        VBox.setMargin(HBOrganiser, new Insets(10, 0, 0, 10)); // Add some left margin

        outerVBox.setAlignment(Pos.CENTER_RIGHT);

        HBOrganiser.getChildren().addAll(lblUsefulData, usefulData);

        alignmentBox.getChildren().addAll(employeeLiveData, HBOrganiser);

        HBox imageOrganiser = new HBox();


        imageOrganiser.getChildren().addAll(imageView, alignmentBox);

        outerVBox.getChildren().addAll(employeeTitle, imageOrganiser);


        return outerVBox;
    }

these labels are being populated by database methods from another class that all take the argument of employeeid. This is its use in the FXMLController java class part of the application:

       @FXML
    public void switchScene(ActionEvent event){
      
          if (event.getSource() == btnDashboard){
              anchDashboard.setVisible(true);
              anchAccounts.setVisible(false);
              anchManageUsers.setVisible(false);
              anchStaff.setVisible(false);
              anchStatistics.setVisible(false);
              anchTimetable.setVisible(false);
              lblTitle.setText("Dashboard");
              
              
          } 
          else if (event.getSource() == btnTimetable) {
              anchDashboard.setVisible(false);
              anchAccounts.setVisible(false);
              anchManageUsers.setVisible(false);
              anchStaff.setVisible(false);
              anchStatistics.setVisible(false);
              anchTimetable.setVisible(true);
              lblTitle.setText("Timetable");
          }
          else if (event.getSource() == btnStaff){
              anchDashboard.setVisible(false);
              anchAccounts.setVisible(false);
              anchManageUsers.setVisible(false);
              anchStaff.setVisible(true);
              anchStatistics.setVisible(false);
              anchTimetable.setVisible(false);
              lblTitle.setText("Staff");
              SPB.generateMultiAnchorPanes(anchStaff);
}
          }
          else if (event.getSource() == btnManageUsers){
              anchDashboard.setVisible(false);
              anchAccounts.setVisible(false);
              anchManageUsers.setVisible(true);
              anchStaff.setVisible(false);
              anchStatistics.setVisible(false);
              anchTimetable.setVisible(false);
              lblTitle.setText("Manage Users");
          }
          else if (event.getSource() == btnStatistics){
              anchDashboard.setVisible(false);
              anchAccounts.setVisible(false);
              anchManageUsers.setVisible(false);
              anchStaff.setVisible(false);
              anchStatistics.setVisible(true);
              anchTimetable.setVisible(false);
              lblTitle.setText("Statistics");
          }
        else if (event.getSource() == btnAccounts){
              anchDashboard.setVisible(false);
              anchAccounts.setVisible(true);
              anchManageUsers.setVisible(false);
              anchStaff.setVisible(false);
              anchStatistics.setVisible(false);
              anchTimetable.setVisible(false);
              lblTitle.setText("Accounts");
          }
                 }

This is the XML code for the button:

<Button fx:id="btnStaff" blendMode="LIGHTEN" mnemonicParsing="false" onAction="#switchScene" style="-fx-background-color: #363232;" text="Staff" textFill="#7c8184">
                 <graphic>
                    <FontIcon iconColor="WHITE" iconLiteral="fas-users" iconSize="21" />
                 </graphic></Button>

This also an example of my retrieval methods used in outerVBox creation.

public static boolean isTransActive(int employeeID){
    
     boolean blnTransisActive = false;

    try (Connection conn = JDBConnection.getConnection()) {
        PreparedStatement stmt = conn.prepareStatement("SELECT TRANSISACTIVE FROM TBLTRANSACTION WHERE EMPLOYEEID =?");
        stmt.setInt(1, employeeID); // Bind the employeeID parameter
     
        ResultSet rs = stmt.executeQuery();

        if (rs.next()) {
            blnTransisActive = rs.getBoolean("TRANSISACTIVE");
        }

        
    } catch (SQLException e) {
        e.printStackTrace();
        
    }

This is the stack trace of the error:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
    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 com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
    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:184)
    at java.base/java.lang.Thread.run(Thread.java:829)

So I first printed what the StaffData.retrieveAllEmployeeIDs() was returning and that was not the error returns correct values. I think it might be the acnhor pane anchstaff that's causing the problem because whenever I get the error if i got into scene builder and stretch the size of anchStaff it starts to work again as intended until but when I try closing and running the application multiple times and pressing the staff button the original exception gets thrown.

I appreciate any help anyone can offer in possible solutions or any theories of what the error could be. Thank you.

  • 2
    Show the whole stack trace. It usually tells you exactly the line that has the problem. – SedJ601 Aug 25 '23 at 16:19
  • In the last block of code there's no return I have it in my code just didn't copy over by accident. – David corcoran Aug 25 '23 at 16:21
  • 2
    See if reading [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/q/3988788/6395627) helps you solve the problem on your own. If not, then please [edit] your question to include the stack trace (as text, in the question itself, formatted as code). – Slaw Aug 25 '23 at 16:30
  • Thank you for responding I have added the stack trace in. – David corcoran Aug 25 '23 at 16:55
  • 2
    The error message is saying that the argument types for the method that is being called are wrong. The stack trace shows this is happening when you press a button that is defined in an FXML file. Show the FXML and the controller method that is referenced by it. – James_D Aug 25 '23 at 17:03
  • Related questions (Using the Google search: https://www.google.com/search?q=site%3Astackoverflow.com+javafx+illegalargumentexception+argument+type+mismatch&oq=site%3Astackoverflow.com+javafx+illegalargumentexception+argument+type+mismatch&aqs=chrome..69i57j69i58.29025j0j7&sourceid=chrome&ie=UTF-8#ip=1) : https://stackoverflow.com/questions/27079062/ https://stackoverflow.com/questions/40398668/ https://stackoverflow.com/questions/56915195/ https://stackoverflow.com/questions/36969189/ https://stackoverflow.com/questions/41408309/ https://stackoverflow.com/questions/59544290/ (and many more) – James_D Aug 25 '23 at 17:19
  • 1
    Check your imports. – SedJ601 Aug 25 '23 at 17:33
  • 1
    Off-topic: 1. don't use a single method as an event handler for multiple controls, and 2. don't write unnecessarily repetitive code – James_D Aug 25 '23 at 17:39
  • As @SedJ601 mentions: make sure you have the correct import for `ActionEvent`. – James_D Aug 25 '23 at 17:48
  • I'm using Event instead of ActionEvent and now its working. – David corcoran Aug 25 '23 at 17:51
  • Why are you using `Event`? `ActionEvent` is the correct parameter type, as long as you import the correct one. (Though if you clean up the code as I suggested, you won't need the parameter at all.) – James_D Aug 25 '23 at 18:00
  • using this "import javafx.event.ActionEvent;" it sometimes dosent work when testing the application multiple times and when using this "import javafx.event.Event;" it works every time. – David corcoran Aug 25 '23 at 18:07
  • Off-topic: It looks like you are switching scenes by loading all of the scenes and then setting the visibility to true only on the scene you want to load. My opinion is that this is bad practice. You should be doing something like https://stackoverflow.com/a/75514636/2423906. – SedJ601 Aug 25 '23 at 18:18
  • I have taken your advice and cut the code down the code using instance variables instead of passing parameters to the method thank you for the help @James_D – David corcoran Aug 25 '23 at 18:21
  • Thank you @SedJ601 for providing that link it is a much more efficient way to do it. – David corcoran Aug 25 '23 at 18:28
  • 2
    If using `ActionEvent` sometimes fails and sometimes doesn't, then that sounds like you're using that method for multiple types of events. E.g., you have both `onAction="#switchScene"` and `onMouseClicked="#switchScene"` in the FXML file. The former is an `ActionEvent`, the latter is a `MouseEvent`. The reason `Event` works is because that's a supertype of both the aforementioned types. But I would suggest keeping event handler methods themselves separate. Note there's nothing that says multiple event handler methods can't **call** the same method (to avoid code duplication). – Slaw Aug 25 '23 at 18:47
  • Thank you @Slaw that makes sense to me now and explains why the code was working sometimes which was the most confusing part. – David corcoran Aug 26 '23 at 01:07

0 Answers0