Reputation: 1
Hello I am new in JavaFx, I'm trying to build a login/signup application, I think my code is correct and my big issue is a NULL statement or a project setup that I can't see to fix. I tried to follow most of the answers in similar questions but I understand I don't have the same issue. Don't mind the italian comments and thank you in advance and kind regards.
Here's the error window I get, as a beginner I got a bit overwhelmed, what seems to be the issue in the main? I'll also include an image of my project setup
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:208)
at com.example.loginsignup/com.example.loginsignup.HelloApplication.start(HelloApplication.java:16)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
Exception running application com.example.loginsignup.HelloApplication
Process finished with exit code 1
Here's my Controller
package com.example.loginsignup;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.net.URL;
import java.util.ResourceBundle;
public class HelloController implements Initializable { //controller per pulsanti login e signup
@FXML
private TextField tf_username;
@FXML
private TextField tf_password;
@FXML
private Button button_login;
@FXML
private Button button_signup;
@Override
public void initialize(URL location, ResourceBundle resourceBundle) { //richiamo metodi per il pulsante di login
button_login.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
DBUtil.logInUser(actionEvent, tf_username.getText(), tf_password.getText()); //passo tutto da DBUtil per il login
}
});
button_signup.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
DBUtil.changeScene(actionEvent,"signup.fxml", "Sign Up", "null");
}
});
}
}
Here's my main
package com.example.loginsignup;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.util.Objects;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root;
root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("sample.fxml")));
stage.setTitle("Log in");
stage.setScene(new Scene(root, 600, 400));
stage.show(); //1:14:24
}
public static void main(String[] args) {
launch(args);
}
}
and the sample.fxml as the front page, I did not put my login and signup controller, DBcontroller nor their respective .fxml files as they are correct
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1">
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="13.0" layoutY="60.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@DominatorLogo.jpg" />
</image>
</ImageView>
</children>
</AnchorPane>
<AnchorPane prefHeight="400.0" prefWidth="446.0">
<children>
<Label layoutX="78.0" layoutY="104.0" prefHeight="44.0" prefWidth="110.0" text="Username">
<font>
<Font size="22.0" />
</font>
</Label>
<Label layoutX="78.0" layoutY="237.0" prefHeight="44.0" prefWidth="110.0" text="Password">
<font>
<Font size="22.0" />
</font>
</Label>
<TextField fx:id="tf_username" layoutX="233.0" layoutY="113.0" promptText="username" />
<TextField fx:id="tf_password" layoutX="234.0" layoutY="247.0" promptText="password" />
<Button fx:id="button_login" layoutX="186.0" layoutY="326.0" mnemonicParsing="false" text="Log in" />
<Label layoutX="70.0" layoutY="369.0" text="Not a User?" />
<Button fx:id="button_signup" layoutX="140.0" layoutY="364.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="62.0" text="Sign up" />
</children>
</AnchorPane>
</children>
</HBox>
Here's the project setup:
Upvotes: 0
Views: 20