Null List is returning. How to solve this?

gopal krishna mareti :

I am trying to get the data from another stage but it returns an empty null set. how to solve this?

when the user clicks the add button a pop up arises where he could enter the details and after clicking the save button the details of the connection will appear in primary stage.

thanks in advance.

panesClass.java

public class PanesClass extends Application {
    ObservableList<Connections> cList = FXCollections.observableArrayList();

    public static void main(String[] args) {
        launch(args);
    }

    @SuppressWarnings("all")
    @Override
    public void start(Stage primaryStage) throws Exception {
        NewConnection newConnection     = new NewConnection();
        SplitPane     root              = new SplitPane();
        AnchorPane    first             = new AnchorPane();
        AnchorPane    second            = new AnchorPane();
        TableView     activeConnections = new TableView();
        HBox          buttonBox         = new HBox();
        BorderPane    topBar            = new BorderPane();
        Button        nConnection       = new Button("+");
        Button        deleteConnection  = new Button("X");
        Button        connect           = new Button("Connect");

        buttonBox.setSpacing(10);
        buttonBox.getChildren().addAll(nConnection, deleteConnection, connect);
        topBar.setTop(buttonBox);

        TableColumn<String, Connections> cNameColoumn = new TableColumn<>("Name");

        cNameColoumn.setCellValueFactory(new PropertyValueFactory<>("cName"));

        TableColumn<String, Connections> cStatusColoumn = new TableColumn<>("Status");

        cStatusColoumn.setCellValueFactory(new PropertyValueFactory<>("cStatus"));
        activeConnections.getColumns().addAll(cNameColoumn, cStatusColoumn);
        activeConnections.setLayoutX(20);
        activeConnections.setLayoutY(40);
        activeConnections.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        activeConnections.setItems(cList);
        first.getChildren().addAll(topBar, activeConnections);
        root.getItems().addAll(first, second);

        Scene sc = new Scene(root, 600, 480);

        primaryStage.setScene(sc);
        primaryStage.show();
        nConnection.setOnAction(new EventHandler<ActionEvent>() {
                                    @Override
                                    public void handle(ActionEvent event) {
                                        cList = newConnection.getConnection();
                                    }
                                });
    }
}

NewConnection.java

public class NewConnection {
    Connections                 connection = null;
    ObservableList<Connections> cList      = FXCollections.observableArrayList();

    public ObservableList<Connections> getConnection() {
        Stage    secondaryStage = new Stage();
        VBox     root           = new VBox();
        GridPane cDetails       = new GridPane();
        HBox     actionButtons  = new HBox();
        Button   connect        = new Button("Connect");
        Button   save           = new Button("Save");
        Button   cancel         = new Button("Cancel");

        actionButtons.getChildren().addAll(connect, save, cancel);
        actionButtons.setSpacing(10);

        Label name = new Label("Username : ");

        cDetails.add(name, 0, 0);

        TextField uName = new TextField();

        cDetails.setHgrow(uName, Priority.ALWAYS);
        cDetails.add(uName, 1, 0);

        Label password = new Label("Password : ");

        cDetails.add(password, 0, 1);

        TextField pwd = new TextField();

        cDetails.add(pwd, 1, 1);

        Label urllink = new Label("URL : ");

        cDetails.add(urllink, 0, 2);

        TextField url = new TextField();

        cDetails.add(url, 1, 2);
        cDetails.setVgap(10);
        cDetails.setStyle("-fx-padding: 10;" + "-fx-border-style: solid inside;" + "-fx-border-width: 1;"
                          + "-fx-border-insets: 5;" + "-fx-border-radius: 5;" + "-fx-border-color: black;");
        root.getChildren().addAll(cDetails, actionButtons);

        Scene sc = new Scene(root, 500, 200);

        secondaryStage.setScene(sc);
        secondaryStage.show();
        save.setOnAction(new EventHandler<ActionEvent>() {
                             @Override
                             public void handle(ActionEvent event) {
                                 connection = new Connections();
                                 connection.setcName(uName.getText());
                                 connection.setPwd(pwd.getText());
                                 connection.setUrl(url.getText());
                                 cList.add(connection);
                                 secondaryStage.close();
                                 System.out.println(cList);
                             }
                         });
        System.out.println(cList);

        return cList;
    }
}

Connections.java

public class Connections {
    private String cName;
    private String cStatus;
    private String url;
    private String pwd;

    public Connections() {}


    public Connections(String cName, String cStatus, String url, String pwd) {
    this.cName = cName;
    this.cStatus = cStatus;
    this.url = url;
    this.pwd = pwd;
    }


    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getcName() {
        return cName;
    }

    public String getcStatus() {
        return cStatus;
    }

    public void setcName(String cName) {
        this.cName = cName;
    }

    public void setcStatus(String cStatus) {
        this.cStatus = cStatus;
    }


    public String getPwd() {
        return pwd;
    }


    public void setPwd(String pwd) {
        this.pwd = pwd;
    }


    @Override
    public String toString() {
    return "Connections [cName=" + cName + ", cStatus=" + cStatus + ", url=" + url + ", pwd=" + pwd + "]";
    }



}

adxl :

You problem is a misunderstanding of how setOnAction() works.

Here is what happening in your program : when you call getConnetions() the setOnAction() will just determine what will be executed when you click on save and then will continue executing the rest of the code, in other words, the code inside the eventHandler will only run when you click save, that's why the outside output and return are null, because cList initial value is null.

The solution is to set cList from the second stage's setOnAction() method.

In PanesClass.java add a getter for cList and change the nConnection's onAction

public ObservableList<Connections> getCList(){ 
     return cList;
}
nConnection.setOnAction(new EventHandler<ActionEvent>() {
                                    @Override
                                    public void handle(ActionEvent event) {
                                    newConnection.addConnection(this);
                                    }
                                });

And in NewConnection.java add a new connection to the original cList from the save button onAction

public void addConnection(PanesClass panesClass) {
        Stage    secondaryStage = new Stage();
        VBox     root           = new VBox();
        GridPane cDetails       = new GridPane();       
        //same code as getConnection()

        secondaryStage.show();
        save.setOnAction( event ->
                           {
                                connection = new Connections();
                                connection.setcName(uName.getText());
                                connection.setPwd(pwd.getText());
                                connection.setUrl(url.getText());
                                //before adding
                                System.out.println("before: "+panesClass.getCList());
                                panesClass.getCList().add(connection);
                                //after adding
                                System.out.println("after: "+panesClass.getCList());
                                secondaryStage.close();
                           });  
}

Hope that helps.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169242&siteId=1