I know this question has been asked many times, half of them its probably caused by forgetting to check the variable names. Have done that even bounced all over different tutorials to find different solutions. Am trying to verify TextField with postgres database so the user can login. If I comment out the password field the code runs and I can get the user name in the console. When I run PasswordField.getText() I get the NullPointerException error. Tried cleaning workspace and recaching nohing worked, the code ran fine before I used scene builder and now not so much. If anyone has any pointers please feel free to share been having a headache for the past week over this and not made much progress :( Edit: Added detailed parts of the file and the stacktrace
Main App.java file
package com.schoolAdmin.app;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.*;
import javafx.stage.Stage;
import java.sql.SQLException;
import javafx.fxml.*;
import java.net.URL;
import com.schoolAdmin.controllers.DependancyInjector;
import com.schoolAdmin.controllers.LoginFXML;
import com.schoolAdmin.psql.Postgres;
import com.schoolAdmin.scenes.Records_Display;
public class App extends Application {
Postgres psql = new Postgres();
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("../fxml/login_screen.fxml"));
Scene loginScene = new Scene(root)
stage.setScene(loginScene);
stage.setResizable(true);
stage.setTitle("Welcome Screen");
stage.show();
}
public static void main(String[] args) {
Postgres.main(args);
launch();
}
}
Controller file for login_Screen.fxml
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.stage.*;
import javafx.event.*;
import java.net.URL;
import java.util.ResourceBundle;
import com.schoolAdmin.modals.AlertModule;
import com.schoolAdmin.psql.Postgres;
import javafx.scene.layout.*;
import javafx.scene.text.*;
public class LoginFXML{
@FXML
private TextField userField;
private PasswordField passField;
@FXML
private URL location;
@FXML
private ResourceBundle resources;
@FXML
private Button subBtn;
@FXML
private void subKey(ActionEvent event) throws Exception{
// userField = new TextField();
// passField = new PasswordField();
// System.out.println(passField.getText().toString());
Postgres psql = new Postgres();
boolean val=psql.validate(userField.getText(), passField.getText());
if(val){
System.out.println(val + "User Authenticated");
} else {
System.out.println("Failed To Authenticate");
}
}
@FXML
void initialize(){
}
public String getUser(){
return this.userField.getText();
}
}
This is the FXML file from scene builder
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.TilePane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<VBox id="VBOX_Parent" alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="840.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.schoolAdmin.controllers.LoginFXML">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Login" textAlignment="CENTER">
<font>
<Font name="Times New Roman" size="36.0" />
</font>
<VBox.margin>
<Insets bottom="30.0" />
</VBox.margin>
</Text>
<ImageView fitHeight="257.0" fitWidth="295.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../resources/2560x1440.png" />
</image>
</ImageView>
<HBox alignment="CENTER">
<children>
<TilePane alignment="CENTER" hgap="10.0" prefColumns="1" prefHeight="200.0" prefRows="3" prefTileHeight="25.0" prefTileWidth="200.0" prefWidth="200.0" vgap="8.0">
<HBox.margin>
<Insets right="10.0" top="10.0" />
</HBox.margin>
<children>
<Label text="UserName" />
<TextField id="user" fx:id="userField" />
<Label text="Password" />
<PasswordField id="pass" fx:id="passField" />
<Button id="submitBtn" fx:id="subBtn" onAction="#subKey" text="Login" />
</children>
</TilePane>
</children>
</HBox>
</children>
</VBox>
Stacktrace for compiler throws java.lang.NullPointerException error on line 101, which is where I try to get the text from PasswordField.
❯ /usr/bin/env /usr/share/OpenJDK_11/jdk-11.0.10+9/bin/java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:33411 --module-path /home/riley/Downloads/Compressed/toolKits/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml -Dfile.encoding=UTF-8 @/tmp/cp_2txpq641q048qqhdhxcg8rt4u.argfile com.schoolAdmin.app.App
DB Connection ok!
Aug 21, 2021 9:19:45 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 16 by JavaFX runtime of version 11.0.2
riley
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
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.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:8879)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.keyReleased(ButtonBehavior.java:161)
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:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
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.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$KeyHandler.process(Scene.java:4058)
at javafx.graphics/javafx.scene.Scene$KeyHandler.access$1500(Scene.java:4004)
at javafx.graphics/javafx.scene.Scene.processKeyEvent(Scene.java:2121)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2595)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:217)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$1(GlassViewEventHandler.java:248)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
at javafx.graphics/com.sun.glass.ui.View.handleKeyEvent(View.java:547)
at javafx.graphics/com.sun.glass.ui.View.notifyKey(View.java:971)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.reflect.InvocationTargetException
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:76)
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:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
... 47 more
Caused by: java.lang.NullPointerException
at com.schoolAdmin.controllers.LoginFXML.subKey(LoginFXML.java:101)
... 58 more