Currently, I am working on an application that includes a UI with a simple login verification system. When I attempt to grab input from the userInput textfield from the StoreUser() method in the controller, I get an error:
Caused by: java.lang.NullPointerException
at com.company.Controller.storePassword(Controller.java:32)
at com.company.Controller.handleButtonAction(Controller.java:49)
... 58 more
These are the classes in question:
Main class:
package com.company;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.sql.SQLException;
public class Main extends Application {
int ScreenWidth = (int) Screen.getPrimary().getBounds().getWidth();
int ScreenHeight = (int) Screen.getPrimary().getBounds().getHeight();
//Verifies that the program has been run before on the computer. Allows for initial logins, first user account creation, etc.
public static void initialLoginVerify() throws IOException {
File f = new File("C:\\453-Project");
boolean path_check = f.exists();
if (!path_check) {
f.mkdir();
}
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent loginPageParent = FXMLLoader.load(getClass().getResource("loginPage.fxml"));
primaryStage.setTitle("Harmony v0.11");
primaryStage.setScene(new Scene(loginPageParent, ScreenWidth, ScreenHeight - 60));
primaryStage.setMaximized(true);
primaryStage.show();
}
//this is where the bulk of the program can start. It allows nothing else about the program to be accessed without
//successfully logging in first
public static void After_Login(){
System.out.println("You logged in successfully :D");
}
public static void main(String[] args) throws IOException {
//test username is john, test password is Doe
launch(args);
}
}
Controller class:
package com.company;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Screen;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML static TextField userInput;
@FXML static PasswordField passwordInput;
@FXML Button logButton;
int ScreenWidth = (int) Screen.getPrimary().getBounds().getWidth();
int ScreenHeight = (int) Screen.getPrimary().getBounds().getHeight();
public static String storePassword() {
System.out.println("inside of storepassword");
String passWord = passwordInput.getText();
return passWord;
}
public static String storeUser() {
System.out.println("inside of storeuser");
String userName = userInput.getText();
System.out.println("Username: " + userName + "\n");
return userName;
}
@FXML
public void handleButtonAction (ActionEvent event) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
storePassword();
storeUser();
Parent mainPageParent = FXMLLoader.load(getClass().getResource("mainPage.fxml"));
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(new Scene(mainPageParent,ScreenWidth,ScreenHeight-60));
app_stage.setMaximized(true);
app_stage.show();
}
@FXML
private void handleLogoutButton (ActionEvent event) throws IOException {
Parent loginPageParent = FXMLLoader.load(getClass().getResource("loginPage.fxml"));
Stage app_Stage2 = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_Stage2.setScene(new Scene(loginPageParent, ScreenWidth, ScreenHeight-60));
app_Stage2.setMaximized(true);
app_Stage2.show();
}
@FXML
private void creationAccountButton (ActionEvent event) throws IOException {
Parent creationPageParent = FXMLLoader.load(getClass().getResource("accountCreation.fxml"));
Stage app_Stage3 = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_Stage3.setScene(new Scene(creationPageParent, ScreenWidth, ScreenHeight-60));
app_Stage3.setMaximized(true);
app_Stage3.show();
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {}
Any help would be greaty appreciated! Thank you so much!