'JavaFX error: Event Handler is not in the namespace
I am creating a JavaFX application. I can start the application and login but when I try to access the Customers Scene, I get the following error:
(Sorry in advance for all the code & errors; didn't want to leave anything out)
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
Here is 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>
Below is my controller class (in progress):
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);
}
}
}
The error says that it cannot find the event handler. I don't know why though because I have attached @FXML to the function. I also tried making the function public (both of these are still suggested by the fxml document which shows an error at line 27--> #deleteCustomerButtonPushed) I have checked all import statements to ensure that the JavaFX version is used.
Solution 1:[1]
The InvocationTargetException you get means that the system was trying to find a particular method but was unable to find a suitable candidate. As the error puts it: "the event handler is not in the Namespace".
The problem comes from the signature of your method:
@FXML private void deleteCustomerButtonPushed(Customer customer) {
//...
}
The argument should be a ActionEvent
like your other methods, not a Customer
.
@FXML private void deleteCustomerButtonPushed(ActionEvent e) {
//...
}
Solution 2:[2]
It can be caused from also
using
import java.awt.event.ActionEvent;
instead of
import javafx.event.ActionEvent;
Solution 3:[3]
The reason I got this error was I missed the @FXML
from the method declaration
it was like this
private void onCheckboxToggle(ActionEvent event) {
Added @FXML
@FXML
private void onCheckboxToggle(ActionEvent event) {
My custom component (nothing speacial):
<CheckBox fx:id="completed" onAction="#onCheckboxToggle" />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Patrick |
Solution 2 | yasineryigit |
Solution 3 | O-9 |