当前位置: 首页 > 知识库问答 >
问题:

JavaFX错误:事件处理程序不在命名空间中

焦正德
2023-03-14

我正在创建一个JavaFX应用程序。我可以启动应用程序并登录,但当我尝试访问客户场景时,会出现以下错误:

(提前为所有代码道歉

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
Caused by: 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 sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 48 more
Caused by: javafx.fxml.LoadException: Error resolving onAction='#deleteCustomerButtonPushed', either the event handler is not in the Namespace or there is an error in the script.
file:/C:/Users/limpFish/Documents/repositories/c195-java-2/C195/dist/run642260308/C195.jar!/c195/CustomersScene.fxml:27

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
    at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at c195.MainSceneController.customersButtonPushed(MainSceneController.java:43)
    ... 58 more

这里是CustomerScene。fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane id="AnchorPane" prefHeight="591.0" prefWidth="475.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="c195.CustomersSceneController">
   <children>
      <VBox layoutY="51.0" prefHeight="541.0" prefWidth="500.0">
         <children>
            <TableView fx:id="customerTableView" prefHeight="470.0" prefWidth="500.0">
              <columns>
                <TableColumn fx:id="customerTableNameTableColumn" prefWidth="75.0" text="Name" />
                <TableColumn fx:id="customerTableAddressTableColumn" prefWidth="302.0" text="Address" />
                  <TableColumn fx:id="customerTablePhoneNumberTableColumn" minWidth="0.0" prefWidth="122.0" text="Phone Number" />
              </columns>
            </TableView>
            <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="100.0">
               <children>
                  <Button fx:id="addCustomerButton" mnemonicParsing="false" onAction="#addCustomerButtonPushed" prefHeight="30.0" prefWidth="60.0" text="Add" />
                  <Button fx:id="modifyCustomerButton" mnemonicParsing="false" onAction="#modifyCustomerButtonPushed" prefHeight="30.0" prefWidth="60.0" text="Modify" />
                  <Button fx:id="deleteCustomerButton" mnemonicParsing="false" onAction="#deleteCustomerButtonPushed" prefHeight="30.0" prefWidth="60.0" text="Delete" />
               </children>
            </HBox>
            <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="260.0">
               <children>
                  <Button fx:id="returnButton" mnemonicParsing="false" onAction="#returnButtonPushed" prefHeight="30.0" prefWidth="60.0" text="Return" />
                  <Button fx:id="quitButton" mnemonicParsing="false" onAction="#quitButtonPushed" prefHeight="30.0" prefWidth="60.0" text="Quit" />
               </children>
            </HBox>
         </children>
      </VBox>
      <Label layoutX="19.0" layoutY="14.0" prefHeight="30.0" prefWidth="91.0" text="Customers">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

以下是我的控制器类(正在进行中):

package c195;

import java.io.IOException;
import java.net.URL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
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.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;


public class CustomersSceneController implements Initializable {

    @FXML private TableView<Customer> customerTableView;
    @FXML private TableColumn<Customer, String> customerTableNameTableColumn;
    @FXML private TableColumn<Customer, String> customerTableAddressTableColumn;
    @FXML private TableColumn<Customer, String> customerTablePhoneNumberTableColumn;
    @FXML private Button addCustomerButton;
    @FXML private Button modifyCustomerButton;
    @FXML private Button deleteCustomerButton;
    @FXML private Button returnButton;
    @FXML private Button quitButton;


    @FXML private void addCustomerButtonPushed(ActionEvent event) throws IOException
    {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("AddCustomerScene.fxml"));
        Parent addCustomerSceneParent = loader.load();
        Scene addCustomerSceneScene = new Scene(addCustomerSceneParent);

        Stage window = (Stage) ((Node)event.getSource()).getScene().getWindow();
        window.setScene(addCustomerSceneScene);
        window.show();
    }

    @FXML private void modifyCustomerButtonPushed(ActionEvent event) throws IOException
    {
        // to do
    }

    @FXML private void deleteCustomerButtonPushed(Customer customer) throws SQLException
    {
        try
        {
            PreparedStatement sql = DatabaseUtils.getDBConnection().prepareStatement("Delete customer.*, address.* from customer inner join address on customer.addressId = address.addressId where customer.customerId = ?");
            sql.setString(1, Integer.toString(customer.getCustomerId()));
            sql.executeUpdate();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    @FXML private void returnButtonPushed(ActionEvent event) throws IOException
    {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("MainScene.fxml"));
        Parent mainSceneParent = loader.load();
        Scene mainSceneScene = new Scene(mainSceneParent);

        Stage window = (Stage) ((Node)event.getSource()).getScene().getWindow();
        window.setScene(mainSceneScene);
        window.show();
    }

    @FXML private void quitButtonPushed(ActionEvent event) throws IOException
    {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION, 
                                "Are you sure you want to quit?", 
                                ButtonType.YES, 
                                ButtonType.NO, 
                                ButtonType.CANCEL);

        alert.setHeaderText("Quit Confirmation");
        alert.showAndWait()
             .filter(response -> response == ButtonType.YES)
             .ifPresent(response -> {
                                     Platform.exit();
                                     System.exit(0);
                                    });
    }

    public ObservableList<Customer> updateCustomerTableView() throws SQLException
    {
        String customerId;
        String name;
        String address;
        String address2;
        String city;
        String country;
        String postalCode;
        String totalAddress;
        String phone;

        ObservableList<Customer> customers = FXCollections.observableArrayList();
        try
        {
            PreparedStatement sql = DatabaseUtils.getDBConnection().prepareStatement("Select customer.customerId, customer.customerName, address.address, address.address2, address.phone, city.city, country.country" +
                                                                                     "from customer" +
                                                                                     "inner join address on customer.addressId = address.addressId" +
                                                                                     "inner join city on address.cityId = city.cityId" +
                                                                                     "inner join country on city.countryId = country.countryId;");
            ResultSet result = sql.executeQuery();

            while (result.next())
            {
                customerId = result.getString("customer.customerId");
                name = result.getString("custumer.customerName");
                address = result.getString("address.address");
                address2 = result.getString("address.address2");
                city = result.getString("city.city");
                country = result.getString("country.country");
                postalCode = result.getString("address.postalCode");
                phone = result.getString("address.phone");

                Customer customer = new Customer(Integer.parseInt(customerId), 
                                                 name,
                                                 address,
                                                 address2,
                                                 city,
                                                 country,
                                                 postalCode, 
                                                 phone);

                customers.add(customer);
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return customers;
    }

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        customerTableNameTableColumn.setCellValueFactory(new PropertyValueFactory<Customer, String>("name"));
        customerTableAddressTableColumn.setCellValueFactory(new PropertyValueFactory<Customer, String>("totalAddress"));
        customerTablePhoneNumberTableColumn.setCellValueFactory(new PropertyValueFactory<Customer, String>("phone"));

        try {
            updateCustomerTableView();
        } catch (SQLException ex) {
            Logger.getLogger(CustomersSceneController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    

}

错误说它找不到事件处理程序。我不知道为什么,因为我已将@FXML附加到函数。我还尝试将函数公开(fxml文档仍然建议这两者,在第27行显示错误-

共有3个答案

何高旻
2023-03-14

我得到这个错误的原因是我错过了方法声明中的@FXML

原来是这样的

private void onCheckboxToggle(ActionEvent event) {

添加@FXML

@FXML
private void onCheckboxToggle(ActionEvent event) {

我的自定义组件(无特定内容):

<CheckBox fx:id="completed" onAction="#onCheckboxToggle" />
杜霍英
2023-03-14

也可能是由于

使用

import java.awt.event.ActionEvent;

而不是

import javafx.event.ActionEvent;
夏侯阳
2023-03-14

您获得的InvocationTargetException表示系统正在尝试查找特定方法,但无法找到合适的候选方法。正如错误所说:“事件处理程序不在命名空间中”。

问题来自您的方法签名:

@FXML private void deleteCustomerButtonPushed(Customer customer) {
    //...
}

与其他方法一样,参数应该是ActionEvent,而不是Customer。

@FXML private void deleteCustomerButtonPushed(ActionEvent e) {
     //...   
}
 类似资料: