error: java.lang.reflect.InvocationTargetException

error: java.lang.reflect.InvocationTargetException

我曾尝试使用 javaFX 创建登录,但是当我尝试测试我的程序时,这个问题 happen:Exception 在应用程序启动方法中(我仍然没有完成 LoginController.java 中的代码,我刚刚为 Login.fxml 制作了 ui)。它在同一个包裹上。

Login.java(我的主):

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);
}  
}

登录控制器:

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>

错误:

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$launchApplication5(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$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(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$null8(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.access00(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

在您收到的错误消息中,有这一行:

Caused by: java.lang.IllegalArgumentException: Can not set java.awt.TextField field ihm_project.LoginController.Email to javafx.scene.control.TextField

因此请确保您的文本字段“电子邮件”的类型为 javafx.scene.control.TextField 而非 java.awt.TextField

并确保 TextField 的类型是正确的类型,删除代码中的这一行 import java.awt.*;,如果没有发生错误,则表示您使用的是正确的类型