0

I'm having major troubles sending some data to another class file called ValuesSubmitted.java, but only from one method located in my main Controller.java.

My problem is that in all other areas of my Controller.java class I can successfully SET and GET values to/from my ValuesSubmitted.java class with no issues. ValuesSubmitted not does have a corresponding FXML element attached to it, as ValuesSubmitted was intended for a data source.

Please follow my code as my comments will explain in further detail.

@FXML

    private void ResetPersonnel(){

        //Instantiating ValuesSubmitted java class
        ValuesSubmitted values = new ValuesSubmitted();

        try (Connection connection = DriverManager.getConnection(CONNECTION_STRING)) {


            //Fetching SQL DB - working as intended
            final String queryTitle = "SELECT * FROM PERSONNEL WHERE TRUCK_TYPE = '" + values.getTypeOfTruck() + "' AND TRUCK_NUMBER = '"
                                      + values.getTruckNumber() + "' AND SHIFT_COLOR = '" + values.getShiftColor()+"';";

            PreparedStatement statementTitle = connection.prepareStatement(queryTitle);

            String resultTitle;
            ResultSet resultSetTitle = statementTitle.executeQuery();
            while (resultSetTitle.next()) {

                // Each result
                resultTitle = resultSetTitle.getString("TITLE") + " " + resultSetTitle.getString("LAST_NAME");

                // THIS ADDS EACH ENTRY FOUND INTO OBSERVABLE "list" variable located in this Controller.java class.(this works.)
                list.add(resultTitle);

                // This is SUPPOSE to add each entry found into an observable list called "personnel" located in a single class file called ValuesSubmitted.java
                // ValuesSubmitted doesn't have a FXML page for it. It was intended for a data storage with all my setters and getters.
                values.setPersonnel(list);

            }

            // When I Sys-Out this line, it returns 0 upon this method being called.
            System.out.println(values.getPersonnel().size());

            // This is my ListView instance object with "list" being the variable of my observable list.
            // This populates my List view container successfully with the observable list located in Controller.java
            crewMembersList.setItems(list);

            /*
            My problem is that in all other areas of my Controller.java class I can successfully SET and GET values in my ValuesSubmitted.java class with no issues.
            Again, ValuesSubmitted not does have a corresponding FXML element attached to it, as ValuesSubmitted was intended for a data source.
             */

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

    }

This is another method located in Controller.java that successfully sets data to the ValuesSubmitted.java class.

@FXML

   private void addCrewMember() {

        //TODO FIX DUPLICATE ENTRIES WITH CASE SENSITIVITY
        TextInputDialog addPersonnel = new TextInputDialog();
        addPersonnel.setTitle("Personnel Names");
        addPersonnel.setHeaderText("Enter personnel title then last name.");
        addPersonnel.setContentText("ex. \"FF Smith\"");
        Optional<String> result = addPersonnel.showAndWait();

        if (result.isPresent() && !addPersonnel.getResult().isEmpty()) {
            values.setPersonnelMembers(addPersonnel.getResult());


        }

    // This line validates that the entry above was indeed added to the list.
        System.out.println(values.getPersonnel());

    }
DAIRAV
  • 723
  • 1
  • 9
  • 31
  • 1
    Have you tried printing anything out within your `while` loop to ensure that `values.setPersonnel()` is actually ever called? Perhaps your `resultSet` is empty? – Zephyr Aug 01 '18 at 00:53
  • Ok I tried printing out in the while loop and nothing printed. I just found out that in my SQL statement in all the spots where I'm calling "values.getVariableName" they are returning null. So I have no clue because I made an instance of the other java class at the top of the method. –  Aug 01 '18 at 01:07
  • Well, without seeing your `ValuesSubmitted` class, we can't really know why that is. But unless you're actually setting those values (`TypeOfTruck`, `TruckNumber`, etc) in the constructor, they would be null. You aren't setting them after the instantiation of the class at least. – Zephyr Aug 01 '18 at 01:10
  • I just realized that truckType, truckNumber, etc. were null because I never stored them in new variables after they were passed in from another controller when I was switching scenes. All is well now. Thanks. –  Aug 01 '18 at 02:11
  • 1
    unrelated: please learn java naming conventions and stick to them – kleopatra Aug 01 '18 at 07:32
  • 1
    Unrelated: Please state what you are referring to instead of throwing out a broad sentence which provides no insight to your reasoning of why you decided to post code critique. –  Aug 01 '18 at 11:42
  • @J.Zimmerman - Method names (`ResetPersonnel()`)should begin with a lowercase letter and be camelCase from there on. Something you would know if you learned Java naming conventions and stuck to them, as suggested. No need to get snarky with the people here trying to help you. – Zephyr Aug 01 '18 at 12:33
  • I'm aware of camelCase naming convention for java. I see that I made a mistake. I didn't scour my code when I came here for camelCase errors before posting as my main reasoning was to figure out the problem. No snark here, I didn't see my mistake when kleopatra posted that comment to see what she was talking about. –  Aug 01 '18 at 13:18

2 Answers2

0

list.add(resultTitle);

I guess this is an intermediate step for assigning the Personnel list in ValuesSubmitted values, it's ok. I recommend checking its size or printing elements first.

For setPersonnel(List list), which seems to be problematic, try copying each element in the temporary list to the ValuesSubmitted.personnelList. There is an addAll method you can use java.util.ArrayList.addAll(Collection<? extends E> c)

So you need something like values.getPersonnel().addAll(list);

golyadkin
  • 84
  • 1
  • 5
0

The difference between those methods is that in the second one values is a field which is probably filled during other user interactions.

In the first method you create a fresh instance stored in a local variable:

ValuesSubmitted values = new ValuesSubmitted();

Probably the properties of this instance are not initialized. You probably need to pass the instance to the controller when loading the fxml, see Passing Parameters JavaFX FXML .

One thing that seems odd is you passing the list in every loop iteration. You should probably move this statement outside of the loop:

values.setPersonnel(list);

Furthermore since list seems to be a field that is reused, it could be necessary to clear it before retrieving the data.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • Thanks, I updated my code to only add the resultTitle string on every iteration to the list and not the list itself. –  Aug 01 '18 at 11:50