簡體   English   中英

JavaFX 錯誤:事件處理程序不在命名空間中

[英]JavaFX error: Event Handler is not in the namespace

我正在創建一個 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

這是 CustomersScene.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>

下面是我的controller class(進行中):

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 附加到 function。我還嘗試公開 function(fxml 文檔仍然建議這兩個,它在第 27 行顯示錯誤--> #deleteCustomerButtonPushed)我有檢查所有導入語句以確保使用 JavaFX 版本。

您獲得的 InvocationTargetException 意味着系統正在嘗試查找特定方法但無法找到合適的候選方法。 正如錯誤所說:“事件處理程序不在命名空間中”。

問題來自您的方法的簽名:

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

參數應該是一個ActionEvent像你的其他方法一樣,而不是一個Customer

@FXML private void deleteCustomerButtonPushed(ActionEvent e) {
     //...   
}

也可能是由

使用

import java.awt.event.ActionEvent;

代替

import javafx.event.ActionEvent;

我收到此錯誤的原因是我錯過了方法聲明中的@FXML

就像這樣

private void onCheckboxToggle(ActionEvent event) {

添加@FXML

@FXML
private void onCheckboxToggle(ActionEvent event) {

我的自定義組件(沒什么特別的):

<CheckBox fx:id="completed" onAction="#onCheckboxToggle" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM