Issue
i have tried to creat a login with javaFX but when i tried to test my program ,this problem happen:Exception in application start method(i still haven't completed the code in LoginController.java , I just made the ui for Login.fxml). it is on the same package.
package ihm_project;
import javafx.application.Application;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.stage.Stage;
public class Login extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent parent = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
LoginController:
package ihm_project;
import java.awt.*;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.*;
public class LoginController implements Initializable {
@FXML
private TextField Email;
@FXML
private TextField Password;
@FXML
private Button Login;
@FXML
private Label Message;
@FXML
public void LoginOnClick(ActionEvent e){
if(Email.getText().isEmpty()&&Password.getText().isEmpty())
{
Message.setText("try to login!");
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
Login.fxml:
<?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.layout.Pane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="585.0" prefWidth="732.0" styleClass="backgrouncolor"
stylesheets="@Design.css" xmlns="http://javafx.com/javafx/17"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="ihm_project.LoginController">
<children>
<Pane layoutX="188.0" layoutY="36.0" prefHeight="447.0" prefWidth="516.0"
styleClass="backgrouncolor2" stylesheets="@Design.css">
<children>
<Label layoutX="42.0" layoutY="57.0" prefHeight="31.0" prefWidth="143.0" text="Hello
There! " textFill="WHITE">
<font>
<Font size="24.0" />
</font>
</Label>
<Label layoutX="42.0" layoutY="92.0" text="Welcome Back" textFill="WHITE">
<font>
<Font size="24.0" />
</font>
</Label>
<TextField fx:id="Email" layoutX="238.0" layoutY="150.0" prefHeight="31.0"
prefWidth="232.0" styleClass="borders" stylesheets="@Design.css" />
<TextField fx:id="Password" layoutX="238.0" layoutY="211.0" prefHeight="31.0"
prefWidth="232.0" styleClass="borders" stylesheets="@Design.css" />
<Label layoutX="174.0" layoutY="150.0" text="Email" textFill="WHITE">
<font>
<Font size="17.0" />
</font>
</Label>
<Label layoutX="159.0" layoutY="214.0" text="password" textFill="WHITE">
<font>
<Font size="17.0" />
</font>
</Label>
<Button layoutX="387.0" layoutY="24.0" mnemonicParsing="false" prefHeight="38.0"
prefWidth="101.0" styleClass="button2" stylesheets="@Design.css" text="Sign up"
textFill="WHITE">
<font>
<Font size="18.0" />
</font></Button>
<Button fx:id="Login" layoutX="379.0" layoutY="311.0" mnemonicParsing="false"
onAction="#LoginOnClick" prefHeight="38.0" prefWidth="101.0" styleClass="button1"
stylesheets="@Design.css" text="Log in" textFill="WHITE">
<font>
<Font size="18.0" />
</font></Button>
<Label fx:id="Message" layoutX="282.0" layoutY="269.0" prefHeight="18.0"
prefWidth="101.0" textFill="#0d2ed3" />
</children></Pane>
<HBox layoutX="19.0" layoutY="293.0" prefHeight="273.0" prefWidth="286.0"
styleClass="backgroundImage" stylesheets="@Design.css">
<children>
<ImageView fitHeight="271.0" fitWidth="286.0" pickOnBounds="true"
preserveRatio="true">
<image>
<Image url="@pic.png" />
</image>
</ImageView>
</children>
</HBox>
</children>
</AnchorPane>
the error:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62
)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
file:/C:/Users/HP/Documents/NetBeansProjects/Ihm_Project/dist/run1839873334/
Ihm_Project.jar!/ihm_project/Login.fxml:27
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at ihm_project.Login.start(Login.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.IllegalArgumentException: Can not set java.awt.TextField field ihm_project.LoginController.Email to javafx.scene.control.TextField
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163)
at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:751)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
... 17 more
Exception running application ihm_project.Login Java Result: 1 Deleting
directory
C:\Users\HP\Documents\NetBeansProjects\Ihm_Project\dist\run1839873334
Solution
In the error message you have, there is this line:
Caused by: java.lang.IllegalArgumentException: Can not set java.awt.TextField field ihm_project.LoginController.Email to javafx.scene.control.TextField
so make sure that your text field "Email" is of type javafx.scene.control.TextField
not java.awt.TextField
and to be sure that the type of TextField is the correct type, delete this line in your code import java.awt.*;
, if no error occurs, it means that you are using the correct type
Answered By - Mark Smith
Answer Checked By - Cary Denson (JavaFixing Admin)