I recently picked up javaFx and I am trying to learn in on my own. I have this few clases that were created to mess around with the GUI I am creating, said classes are:
Library, AccHolder, Bibliography. A library can have many AccHolders and Many Bibliography, then I have these two classes:
import java.util.ArrayList;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application{
Stage window;
Button b1;
AccHolder userInputFromAcc;
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage){
Library lib = new Library();
window = primaryStage;
window.setTitle("Main Window");
b1 = new Button("Press Me!");
b1.setOnAction(
e->{
userInputFromAcc = MainWindow.display("New Card Holder");
lib.addAccount(userInputFromAcc);
lib.printAllAccounts();
});
StackPane layout = new StackPane();
layout.getChildren().add(b1);
Scene scene = new Scene(layout,1300, 650);
window.setScene(scene);
window.show();
}
}
and
import java.util.ArrayList;
import javafx.beans.binding.BooleanBinding;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
public class MainWindow {
private static AccHolder x;
public static AccHolder display(String title){
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(title);
ArrayList<String> list = new ArrayList<>();
GridPane grid1 = new GridPane();
grid1.setPadding(new Insets(10,10,10,10));
grid1.setVgap(8);
grid1.setHgap(10);
//Aplicants's name and text field:
Label apNameLabel = new Label("Aplicant's Name: ");
GridPane.setConstraints(apNameLabel, 0,0);
TextField apNameInput = new TextField();
GridPane.setConstraints(apNameInput, 2,0);
apNameInput.setPromptText("Name");
//Aplicant's age
Label apAgeLabel = new Label("Aplicant's Age: ");
GridPane.setConstraints(apAgeLabel, 0,1);
TextField apAgeInput = new TextField();
GridPane.setConstraints(apAgeInput, 2,1);
apAgeInput.setPromptText("Enter Age ");
//Aplicant's ID card
Label apIDlabel = new Label("Aplicant's ID Card Number: ");
GridPane.setConstraints(apIDlabel, 0,2);
TextField apIDInput = new TextField();
GridPane.setConstraints(apIDInput, 2,2);
apIDInput.setPromptText("Enter ID ");
Button b1 = new Button("Create Account");
GridPane.setConstraints(b1, 0,6);
BooleanBinding bb= new BooleanBinding(){
{
super.bind(apNameInput.textProperty(),
apAgeInput.textProperty(),
apIDInput.textProperty());
}
@Override
protected boolean computeValue() {
return (apNameInput.getText().isEmpty()
|| apAgeInput.getText().isEmpty()
|| apIDInput.getText().isEmpty());
}
};
b1.disableProperty().bind(bb);
b1.setOnAction(e->{
String a,c;
int b = 0;
a = apNameInput.getText();
try{
b = Integer.parseInt(apAgeInput.getText());
} catch(Exception e1){
apAgeInput.setText("");
}
c = apIDInput.getText();
// Save User Input into an array list.
x = new AccHolder(a, b, c);
window.close();
} );
Button b2 = new Button("Close");
GridPane.setConstraints(b2, 1,6);
b2.setOnAction(e-> window.close() );
grid1.getChildren().addAll(apNameLabel, apNameInput, apAgeLabel, apAgeInput, apIDlabel, apIDInput, b1, b2);
Scene scene = new Scene(grid1, 500, 200);
window.setScene(scene);
window.setResizable(false);
window.show();
return x;
}
}
Basically the button in the main opens the second window, that window has the textfields to be filled with the user info, whati intended was to return those values after being collected and create an object of tipe AccHolder in the Main and save it to lib.
I ran into several complications, one of them being that as soon as the first button is pressed the program gives me an error as if the information is not being collected from Window 2 and passed to Window 1.
Notice that I never wanted to create an object in window 2, my main idea was to collect info in its pure form and then pass it to window 1, then create the object in window 1(Main).
I am really new to javaFx and lanmda expressions, therefore I might not be using them effectively. Here is a part of the error that prints out as soon as I press b1 on window 1:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at Library.printAllAccounts(Library.java:45)
at Main.lambda$0(Main.java:30)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)...
etc
Have no clue why is this happening, this is really stalling my learning process.