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.
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) {
//...
}
It can be caused from also
using
import java.awt.event.ActionEvent;
instead of
import javafx.event.ActionEvent;
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" />
Related
After selecting date on my DatePicker I see those errors:
E:\все jdk\jdk1.8.0_152\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3.8\bin" -Dfile.encoding=UTF-8 -classpath "E:\все jdk\jdk1.8.0_152\jre\lib\charsets.jar;E:\все jdk\jdk1.8.0_152\jre\lib\deploy.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\access-bridge-64.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\cldrdata.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\dnsns.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\jaccess.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\jfxrt.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\localedata.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\nashorn.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\sunec.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\sunjce_provider.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\sunmscapi.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\sunpkcs11.jar;E:\все jdk\jdk1.8.0_152\jre\lib\ext\zipfs.jar;E:\все jdk\jdk1.8.0_152\jre\lib\javaws.jar;E:\все jdk\jdk1.8.0_152\jre\lib\jce.jar;E:\все jdk\jdk1.8.0_152\jre\lib\jfr.jar;E:\все jdk\jdk1.8.0_152\jre\lib\jfxswt.jar;E:\все jdk\jdk1.8.0_152\jre\lib\jsse.jar;E:\все jdk\jdk1.8.0_152\jre\lib\management-agent.jar;E:\все jdk\jdk1.8.0_152\jre\lib\plugin.jar;E:\все jdk\jdk1.8.0_152\jre\lib\resources.jar;E:\все jdk\jdk1.8.0_152\jre\lib\rt.jar;C:\Users\ogobe\IdeaProjects\YSK_1\out\production\YSK_1;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3.8\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain sample.Main
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:8413)
at com.sun.javafx.scene.control.skin.DatePickerSkin.handleControlPropertyChanged(DatePickerSkin.java:147)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener$61(BehaviorSkinBase.java:197)
at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(MultiplePropertyChangeListenerHandler.java:55)
at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.control.ComboBoxBase.setValue(ComboBoxBase.java:150)
at com.sun.javafx.scene.control.skin.DatePickerContent.selectDayCell(DatePickerContent.java:689)
at com.sun.javafx.scene.control.skin.DatePickerContent.lambda$createDayCells$174(DatePickerContent.java:731)
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.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$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
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)
... 60 more
Caused by: java.lang.NullPointerException
at sample.Controller.getDate(Controller.java:72)
... 70 more
number of years: 1
number of months: 0
number of days: 0
Process finished with exit code 0
Controller.java:
package sample;
import java.net.URL;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.time.Period;
import javax.swing.*;
public class Controller {
#FXML
private ResourceBundle resources;
#FXML
private URL location;
#FXML
private DatePicker date_1;
#FXML
private DatePicker date_2;
#FXML
private Button buttonAdd;
#FXML
private ChoiceBox<?> choiceBox;
#FXML
private TextField lavozm;
#FXML
private Label year;
#FXML
private Label month;
#FXML
private Label yearOfIIN;
#FXML
private Label monthOfXis;
#FXML
private Label dayOfXis;
public void getDate(ActionEvent event) {
LocalDate dt1 = date_1.getValue();
Calendar c = Calendar.getInstance();
c.set(dt1.getYear(), dt1.getMonthValue(), dt1.getDayOfMonth());
Date date1 = c.getTime();
LocalDate dt2 = date_2.getValue();
Calendar c2 = Calendar.getInstance();
c2.set(dt2.getYear(), dt2.getMonthValue(), dt2.getDayOfMonth());
Date date2 = c.getTime();
Period age = Period.between(dt1, dt2);
int years = age.getYears();
int months = age.getMonths();
int days = age.getDays();
System.out.println("number of years: " + years);
System.out.println("number of months: " + months);
System.out.println("number of days: " + days);
}
#FXML
void initialize() {
}
}
Main.java:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("YSK");
primaryStage.setScene(new Scene(root, 800, 550));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
sample.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="829.0" xmlns="javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="68.0" prefWidth="829.0" style="-fx-background-color: #1d90ed;">
<children>
<TextField layoutX="521.0" layoutY="21.0" prefHeight="16.0" prefWidth="269.0" promptText="Ходим исм шарфи" style="-fx-background-color: #EAFAFA;" />
</children>
</AnchorPane>
<DatePicker fx:id="date_1" layoutX="14.0" layoutY="123.0" onAction="#getDate" prefHeight="27.0" prefWidth="149.0" promptText="Боши" />
<DatePicker fx:id="date_2" layoutX="176.0" layoutY="124.0" onAction="#getDate" prefHeight="27.0" prefWidth="149.0" promptText="Якуни" />
<Button fx:id="buttonAdd" layoutX="387.0" layoutY="172.0" minWidth="26.0" mnemonicParsing="false" prefHeight="17.0" prefWidth="56.0" style="-fx-background-color: #1d90ed;" text="+" />
<ChoiceBox fx:id="choiceBox" layoutX="332.0" layoutY="124.0" prefHeight="26.0" prefWidth="48.0" />
<Text layoutX="28.0" layoutY="438.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Календар хисобида:" />
<Text layoutX="65.0" layoutY="467.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Имтиез блан:" />
<TextField fx:id="lavozm" layoutX="395.0" layoutY="124.0" prefHeight="26.0" prefWidth="185.0" promptText="Лавозм" />
<Text layoutX="624.0" layoutY="143.0" strokeType="OUTSIDE" strokeWidth="0.0" text="йил" />
<Text layoutX="750.0" layoutY="144.0" strokeType="OUTSIDE" strokeWidth="0.0" text="кун" />
<Text layoutX="689.0" layoutY="143.0" strokeType="OUTSIDE" strokeWidth="0.0" text="ой" />
<Label fx:id="year" layoutX="587.0" layoutY="126.0" prefHeight="25.0" prefWidth="28.0" />
<Label fx:id="month" layoutX="654.0" layoutY="127.0" prefHeight="25.0" prefWidth="28.0" />
<Label fx:id="month" layoutX="715.0" layoutY="127.0" prefHeight="25.0" prefWidth="28.0" />
<Text layoutX="187.0" layoutY="437.0" strokeType="OUTSIDE" strokeWidth="0.0" text="йил" />
<Text layoutX="313.0" layoutY="438.0" strokeType="OUTSIDE" strokeWidth="0.0" text="кун" />
<Text layoutX="252.0" layoutY="437.0" strokeType="OUTSIDE" strokeWidth="0.0" text="ой" />
<Label fx:id="year" layoutX="150.0" layoutY="420.0" prefHeight="25.0" prefWidth="28.0" />
<Label fx:id="month" layoutX="217.0" layoutY="421.0" prefHeight="25.0" prefWidth="28.0" />
<Label fx:id="month" layoutX="278.0" layoutY="421.0" prefHeight="25.0" prefWidth="28.0" />
<Text layoutX="191.0" layoutY="466.0" strokeType="OUTSIDE" strokeWidth="0.0" text="йил" />
<Text layoutX="317.0" layoutY="467.0" strokeType="OUTSIDE" strokeWidth="0.0" text="кун" />
<Text layoutX="256.0" layoutY="466.0" strokeType="OUTSIDE" strokeWidth="0.0" text="ой" />
<Label fx:id="yearOfIIN" layoutX="154.0" layoutY="449.0" prefHeight="25.0" prefWidth="28.0" />
<Label fx:id="monthOfXis" layoutX="221.0" layoutY="450.0" prefHeight="25.0" prefWidth="28.0" />
<Label fx:id="dayOfXis" layoutX="282.0" layoutY="450.0" prefHeight="25.0" prefWidth="28.0" />
</children>
</AnchorPane>
What is going wrong
dt2 is null, which makes sense because the getDate method in invoked as the onAction handler referenced in fxml for both date_1 and date_2 date pickers.
Whichever one you use to pick a date first, the other will have a null value for the picked date because you can only pick one date at a time.
How to fix it
If either date_1.getValue() or date_2.getValue() is null, the getDate method should be changed to exit (via return).
The method appears to be calculating the period between the two dates (which doesn't make sense for a null date).
Once the user has picked both dates, the call would go through and the period would be calculated correctly.
At the beginning of getDate(), write:
if (date_1.getValue() == null || date_2.getValue() == null) {
System.out.println(“Both dates not yet chosen, will not calculate period”);
return;
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I'm working on JavaFX and SQL project.
First of all login window opens and if the username and password matches to one stored in my databases then AddCustomer deatils windows will pop up[Works fine till here] . A table would be in my database for the user entry. But as I click on 'save' button I get an error like this
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: java.lang.NullPointerException
at sample.MysqlConnector.<init>(MysqlConnector.java:22)
at sample.AddCustomer.addcustomer(AddCustomer.java:52)
... 58 more
My Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.*;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("login1.fxml"));
primaryStage.setTitle("NOVOTEL HOTEL ");
primaryStage.setScene(new Scene(root, 500, 475));
primaryStage.show();
}
catch(Exception e )
{
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
My MysqlConnector.java
package sample;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import javafx.fxml.FXML;
import java.sql.*;
public class MysqlConnector {
#FXML
public static Statement stmt;
#FXML
public static Connection conn;
public MysqlConnector() {
Connector();
String TableName = "CustomerDetails";
try {
stmt = conn.createStatement();
DatabaseMetaData dbm = conn.getMetaData();
ResultSet tables = dbm.getTables(null, null, TableName.toUpperCase(), null);
if (tables.next()) {
System.out.println("Ready to go");
} else {
stmt.execute("CREARE TABLE " + TableName + "("
+ " customername varchar(45) primary key,"
+ " customername int" +
"+ customernumber int" +
"+ customerstay int" +
"+ customerguests varchar(45)"
+ ")");
}
} catch (SQLException e1) {
System.err.println(e1.getMessage());
}
}
#Nullable
#NotNull
public static Connection Connector() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "password");
return conn;
} catch (Exception e) {
return null;
}
}
#NotNull
#Nullable
public static ResultSet executeQuery(String query) {
ResultSet result ;
try {
stmt = conn.createStatement();
result=stmt.executeQuery(query);
} catch (SQLException e) {
System.out.println("Error here");
e.printStackTrace();
return null;
}
return result;
}
#NotNull
#Nullable
public boolean executeAction(String qu) {
try {
stmt = conn.createStatement();
stmt.execute(qu);
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
My LoginModel.java
package sample;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import javafx.fxml.FXML;
import java.sql.*;
public class MysqlConnector {
#FXML
public static Statement stmt;
#FXML
public static Connection conn;
public MysqlConnector() {
String TableName = "CustomerDetails";
try {
stmt = conn.createStatement();
DatabaseMetaData dbm = conn.getMetaData();
ResultSet tables = dbm.getTables(null, null, TableName.toUpperCase(), null);
if (tables.next()) {
System.out.println("Ready to go");
} else {
stmt.execute("CREARE TABLE " + TableName + "("
+ " customername varchar(45) primary key,"
+ " customername int" +
"+ customernumber int" +
"+ customerstay int" +
"+ customerguests varchar(45)"
+ ")");
}
} catch (SQLException e1) {
System.err.println(e1.getMessage());
}
}
#Nullable
#NotNull
public static Connection Connector() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "password");
return conn;
} catch (Exception e) {
return null;
}
}
#NotNull
#Nullable
public static ResultSet executeQuery(String query) {
ResultSet result ;
try {
stmt = conn.createStatement();
result=stmt.executeQuery(query);
} catch (SQLException e) {
System.out.println("Error here");
e.printStackTrace();
return null;
}
return result;
}
#NotNull
#Nullable
public boolean executeAction(String qu) {
try {
stmt = conn.createStatement();
stmt.execute(qu);
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
My Controller class
import com.sun.istack.internal.Nullable;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.geometry.*;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.*;
public class Controller implements Initializable {
#NotNull
#Nullable
public LoginModel loginModel = new LoginModel();
#FXML
private Label isConnected,db;
#FXML
private TextField txtUsername;
#FXML
private TextField txtpassword;
#FXML
Button abutton = new Button("Login");
private AddCustomer addCustomer;
#Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
if (loginModel.isDBConnected()) {
db.setText("Connected to DATABASE");
} else {
db.setText("Not Connected");
}
}
public void Login (ActionEvent event ) {
try {
if (loginModel.isLogin(txtUsername.getText(), txtpassword.getText())) {
isConnected.setText("Correct");
try {
FXMLLoader addCustomer= new FXMLLoader(getClass().getResource("/sample/addcustomer.fxml"));
Parent root1 = (Parent)addCustomer.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.setTitle("ENTRY RECORD");
stage.show();
}
catch (Exception e){
e.printStackTrace();
}
} else {
Alert alert=new Alert(Alert.AlertType.ERROR);
alert.setHeaderText(null);
alert.setContentText("YOU ARE NOT AUTHENTICATED");
alert.show();
}
} catch (SQLException e) {
isConnected.setText("Incorrect");
e.printStackTrace();
}
}
}
FXML of COntroller Class
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="335.0" prefWidth="526.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Label id="isConnected" fx:id="isConnected" layoutX="69.0" layoutY="27.0" prefHeight="45.0" prefWidth="165.0" textAlignment="CENTER" textFill="#e81313">
<font>
<Font size="15.0" />
</font></Label>
<TextField fx:id="txtUsername" accessibleRole="MENU" layoutX="152.0" layoutY="124.0" promptText="Username" />
<Button fx:id="abutton" layoutX="201.0" layoutY="227.0" mnemonicParsing="false" onAction="#Login" text="Login" />
<PasswordField fx:id="txtpassword" layoutX="152.0" layoutY="154.0" promptText="Password" />
<Label fx:id="db" layoutX="247.0" layoutY="41.0" text="status">
<font>
<Font size="15.0" />
</font>
</Label>
</children>
</AnchorPane>
My AddCustomer class
Here I haven't add functionality for cancel button yet, just want to check 'save' button first.
package sample;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class AddCustomer {
#FXML
public TextField number;
#FXML
public TextField name;
#FXML
public TextField stay;
#FXML
public TextField noofguests;
#FXML
public Button savebutton;
#FXML
public Button cancelbutton;
#FXML
#NotNull
#Nullable
public void addcustomer(ActionEvent actionEvent) throws Exception {
String customername = name.getText();
String customernumber = number.getText();
String customerstay = stay.getText();
String customerguests = noofguests.getText();
if (customername.isEmpty() || customerguests.isEmpty() || customernumber.isEmpty() || customerstay.isEmpty()) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText(null);
alert.setContentText("PLEASE ENTER ALL THE FIELDS");
alert.show();
return;
}
String qu = "INSERT INTO CustomerDetails VALUES(" + "'" + customername + "'," +
"'" + customernumber + "'," +
"'" + customerstay + "'," +
"'" + customerguests + "'" + ")";
System.out.println(qu);
#NotNull
#Nullable
MysqlConnector mysqlConnector=new MysqlConnector();
try {
if (mysqlConnector.executeAction(qu)) {
#NotNull
#Nullable
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setHeaderText(null);
alert.setContentText("SUCCESS");
alert.show();
} else {
#NotNull
#Nullable
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText(null);
alert.setContentText("FAILED");
alert.show();
}
}
catch(Exception e)
{ e.printStackTrace();}
}
public void cancel(ActionEvent actionEvent) {
}
}
My Add Customer FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="599.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.AddCustomer">
<children>
<VBox layoutX="44.0" prefHeight="400.0" prefWidth="513.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="44.0" AnchorPane.rightAnchor="43.0" AnchorPane.topAnchor="0.0">
<children>
<TextField fx:id="name" prefHeight="50.0" prefWidth="492.0" promptText="Customer Name">
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="30.0" />
</VBox.margin>
</TextField>
<TextField fx:id="number" layoutX="10.0" layoutY="10.0" prefHeight="53.0" prefWidth="492.0" promptText="Customer Number">
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</VBox.margin>
</TextField>
<TextField fx:id="stay" layoutX="10.0" layoutY="10.0" prefHeight="52.0" prefWidth="492.0" promptText="Stay Period ">
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</VBox.margin>
</TextField>
<TextField fx:id="noofguests" layoutX="10.0" layoutY="35.0" prefHeight="50.0" prefWidth="492.0" promptText="No. Of Guests">
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</VBox.margin>
</TextField>
<Button fx:id="savebutton" mnemonicParsing="false" onAction="#addcustomer" prefHeight="47.0" prefWidth="109.0" text="Save">
<VBox.margin>
<Insets bottom="10.0" left="100.0" right="10.0" top="10.0" />
</VBox.margin>
</Button>
<Button fx:id="cancelbutton" mnemonicParsing="false" onAction="#cancel" prefHeight="46.0" prefWidth="110.0" text="Cancel">
<VBox.margin>
<Insets bottom="10.0" left="300.0" right="10.0" top="-57.0" />
</VBox.margin>
</Button>
</children>
</VBox>
</children>
</AnchorPane>
Please help in getting this error fixed.
The reason you're getting this error is because you're trying to call conn.createStatement(); on conn which is never initialized and therefore null.
I see you have a function called Connector() which will initialize the variable conn but you are never calling it, a fix would be to call Connector() inside the constructor of MysqlConnector before you're trying to do anything with the variable conn.
public MysqlConnector() {
conn = Connector();
if (conn != null) {
// Do your things here
}
}
Now I'm making a project about File Manager software by JavaFX but a plenty of error occur.
I have some class:
File Main.java
package QLF;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application
{
#Override
public void start(Stage primaryStage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("LogIn.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
File Controller.java
package QLF;
import javafx.scene.control.*;
import javafx.fxml.*;
import javafx.event.*;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable
{
#FXML
private Button btnSignIn;
#FXML
private Button btnQuit;
#FXML
private TextField txtUserName;
#FXML
private PasswordField txtPassword;
#FXML
private Label lblUStatus;
#FXML
private Label lblPStatus;
#FXML
private void handleEvent(ActionEvent event)
{
if(event.getSource() == btnSignIn)
{
//check 'null'
if(txtUserName.getText().equals(null))
{
lblUStatus.setVisible(true);
lblUStatus.setText("You have to enter your Username or your Email");
}
else if(txtPassword.getText().equals(null))
{
lblPStatus.setVisible(true);
lblPStatus.setText("You have to enter your Password");
}
}
if(event.getSource() == btnQuit)
{
}
}
#Override
public void initialize(URL location, ResourceBundle resources)
{
}
}
File LogIn.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.PasswordField?> <?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="376.167" prefWidth="614.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="QLF.Controller">
<children>
<Label id="title" layoutX="141.0" layoutY="55.0" prefHeight="73.0" prefWidth="315.0" text="Log-In to QLF">
<font>
<Font size="50.0" />
</font>
</Label>
<TextField id="txtUserName" fx:id="txtUserName" layoutX="50.0" layoutY="150.0" prefHeight="25.0" prefWidth="521.0" promptText="Username*" />
<Button id="btnSignIn" fx:id="btnSignIn" layoutX="50.0" layoutY="302.0" mnemonicParsing="false" onMouseClicked="#handleEvent" prefHeight="25.0" prefWidth="109.0" text="Sign-In" />
<Button id="btnQuit" fx:id="btnQuit" layoutX="462.0" layoutY="302.0" mnemonicParsing="false" onMouseClicked="#handleEvent" prefHeight="25.0" prefWidth="109.0" text="Quit" />
<PasswordField id="txtPassword" fx:id="txtPassword" layoutX="50.0" layoutY="243.0" prefHeight="25.0" prefWidth="521.0" promptText="Password*" />
<Label fx:id="lblUStatus" layoutX="50.0" layoutY="180.0" prefHeight="17.0" prefWidth="33.0" text="Status" textFill="RED" visible="false" />
<Label fx:id="lblPStatus" layoutX="53.0" layoutY="274.0" text="Status" textFill="#f50000" visible="false" />
</children>
</AnchorPane>
Error
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
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)
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:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
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:381)
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$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:748)
onMouseClicked events cause MouseEvents not ActionEvents. Since MouseEvent cannot be cast to ActionEvent, handleEvent cannot handle MouseEvents.
Use the onAction event handler instead:
...
<Button ... onAction="#handleEvent" ... text="Sign-In" />
<Button ... onAction="#handleEvent" ... text="Quit" />
...
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I want my progressbar to display the values of a method thats returns double values but it doesnt work. But I dont think thats the main problem bc even when I just use a specific number it doesnt work either. Please help me, thanks in advance. That's specific question so I searched cery long without results so if you know a better to do what I want to do or maybe know a website that could help pls link it or tell me.
My Controller for my fxml file(the relevant code is marked):
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.ArrayList;
import java.util.Random;
public class Controller {
playercard iceborn = new playercard(3, false, true, 0);
playercard fireball = new playercard(5, true, false, 0);
playercard warmogs = new playercard(0, false, false, 10);
ArrayList<playercard> playerhand = new ArrayList<>();
*******************************
public void add() {
playerhand.add(iceborn);
playerhand.add(fireball);
playerhand.add(warmogs);
}
public playercard output() {
Random rand = new Random();
int xzufallszahl = rand.nextInt(3);
int zufallszahl = xzufallszahl;
return playerhand.get(zufallszahl);
}
*********************************
#FXML
Stage stage;
public Button Startgameb;
#FXML
public ProgressBar enemyhpnow;
#FXML
public void Startgamebaction(ActionEvent event) throws IOException{
***************
add();
**************
Parent root = FXMLLoader.load(getClass().getResource("Menu.fxml"));
Pane root1 = FXMLLoader.load(getClass().getResource("Tutorial.fxml"));
Pane root2 = FXMLLoader.load(getClass().getResource("Game.fxml"));
Scene tutorialscene = new Scene(root1, 900, 600);
Scene gamescene = new Scene(root2, 900, 600);
Image card = new Image("sample/card.jpg");
ImageView firstplayercard = new ImageView();
firstplayercard.setImage(card);
firstplayercard.setFitWidth(120);
firstplayercard.setFitHeight(160);
firstplayercard.setLayoutX(50);
firstplayercard.setLayoutY(430);
ImageView secondplayercard = new ImageView();
secondplayercard.setImage(card);
secondplayercard.setFitWidth(120);
secondplayercard.setFitHeight(160);
secondplayercard.setLayoutX(175);
secondplayercard.setLayoutY(430);
ImageView thirdplayercard = new ImageView();
thirdplayercard.setImage(card);
thirdplayercard.setFitWidth(120);
thirdplayercard.setFitHeight(160);
thirdplayercard.setLayoutX(300);
thirdplayercard.setLayoutY(430);
ImageView fourthplayercard = new ImageView();
fourthplayercard.setImage(card);
fourthplayercard.setFitWidth(120);
fourthplayercard.setFitHeight(160);
fourthplayercard.setLayoutX(425);
fourthplayercard.setLayoutY(430);
ImageView fifthplayercard = new ImageView();
fifthplayercard.setImage(card);
fifthplayercard.setFitWidth(120);
fifthplayercard.setFitHeight(160);
fifthplayercard.setLayoutX(550);
fifthplayercard.setLayoutY(430);
root2.getChildren().addAll(firstplayercard, secondplayercard, thirdplayercard, fourthplayercard, fifthplayercard);
**********************************
firstplayercard.setOnMouseClicked(e ->{
enemyhpnow.setProgress(output().activate());
});
**********************************
Stage stage;
stage=(Stage) Startgameb.getScene().getWindow();
stage.setScene(gamescene);
stage.show();
}
}
That's the class I made the object from which values I want to use:
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.ProgressBar;
public class playercard {
int dmgovertime;
boolean fire;
boolean ice;
int getlife;
int playerhp = 100;
int enemyhp = 300;
public playercard(int dmgovertime, boolean fire, boolean ice, int getlife) {
this.dmgovertime = dmgovertime;
this.fire = fire;
this.ice = ice;
this.getlife = getlife;
}
public double activate(){
enemyhp = enemyhp - dmgovertime + getlife;
double xenemyhp = enemyhp;
System.out.println(enemyhp);
return xenemyhp;
}
}
That's my fxml file (the relevant one):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="900.0" styleClass="hintergrund1" stylesheets="#style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ProgressBar fx:id="playerhp" layoutX="100.0" layoutY="20.0" prefHeight="20.0" prefWidth="700.0" progress="0.87" />
<Label layoutX="425.0" layoutY="-14.0" prefHeight="50.0" prefWidth="50.0" text="100 HP" />
<ImageView fitHeight="351.0" fitWidth="373.0" layoutX="273.0" layoutY="40.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#enemy.png" />
</image>
</ImageView>
<Label layoutX="812.0" layoutY="15.0" prefHeight="50.0" prefWidth="66.0" text="1/3" />
<Label layoutX="14.0" layoutY="11.0" prefHeight="59.0" prefWidth="77.0" text="1/5" />
<ProgressBar fx:id="enemyhpnow" layoutX="283.0" layoutY="391.0" prefHeight="31.0" prefWidth="353.0" progress="0.75" />
<Label layoutX="431.0" layoutY="374.0" text="250 HP" />
<ImageView fitHeight="160.0" fitWidth="120.0" layoutX="758.0" layoutY="422.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#carddeck.png" />
</image>
</ImageView>
</children>
</Pane>
My Main:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Menu.fxml"));
Scene menuscene = new Scene(root, 900, 600);
primaryStage.setTitle("Project: Spark!");
primaryStage.setScene(menuscene);
primaryStage.show();
}
}
Thats the error I get with enemyhpnow.setProgress(output().activate()); from my Controller:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at sample.Controller.lambda$Startgamebaction$0(Controller.java:100)
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:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
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:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Process finished with exit code 0
And thats the error code when I use enemyhpnow.setProgress(1); also in my Controller:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at sample.Controller.lambda$Startgamebaction$0(Controller.java:100)
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:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
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:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Process finished with exit code 0
You forgot to define your Controller I do not see it anywhere in your code fxml:
fx:controller="yourController"
Why doesn't this work? I'm trying to change stage from login screen to sample screen. Getting an error when trying to use ActionEvent connected to Submit button.
Caused by: java.lang.NullPointerException: Location is required.
Main:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
Stage stage;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
stage = primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("fxml/login.fxml"));
stage.setTitle("Delos");
Scene scene = new Scene(root, 300, 250);
stage.setScene(scene);
stage.show();
}
}
Controller:
package controllers;
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.TextField;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable{
//#FXML private TextField username;
#FXML
private void handleLogin(javafx.event.ActionEvent event) throws IOException {
Parent blah = FXMLLoader.load(getClass().getResource("fxml/sample.fxml"));
Scene scene = new Scene(blah);
Stage appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
appStage.setScene(scene);
appStage.show();
}
/**public void handleLogin(ActionEvent actionEvent) {
System.out.printf("You are logged in %s! %n", username.getText());
}*/
#Override
public void initialize(URL location, ResourceBundle resources) {
}
}
Login.fxml:
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>
<GridPane fx:controller="controllers.Controller"
stylesheets="/css/login.css"
xmlns:fx="http://javafx.com/fxml">
<children>
<Text text="Sign In"
styleClass="title"
GridPane.rowIndex="0"
GridPane.columnSpan="2"
GridPane.halignment="CENTER"/>
<Label text="Username:"
GridPane.rowIndex="1"
GridPane.columnIndex="0"/>
<TextField fx:id="username"
GridPane.rowIndex="1"
GridPane.columnIndex="1"/>
<Label text="Password:"
GridPane.rowIndex="2"
GridPane.columnIndex="0"/>
<TextField fx:id="password"
GridPane.rowIndex="2"
GridPane.columnIndex="1"/>
<Pane GridPane.rowIndex="3">
<children>
<Button text="Login"
onAction="#handleLogin"
GridPane.rowIndex="3"
GridPane.columnIndex="1"
GridPane.halignment="LEFT"/>
</children>
</Pane>
</children>
</GridPane>
sampleController:
package controllers;
/**
* Created by Basse on 22.11.2016.
*/
import javafx.fxml.Initializable;
import java.net.URL;
import java.util.ResourceBundle;
public class sampleController implements Initializable {
#Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println("View is now loaded!");
}
}
Sample.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml"
fx:controller="controllers.sampleController">
<Label text="I love bacon"/>
<Button text="Submit"/>
</VBox>
Edit:
/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/bin/java -Didea.launcher.port=7537 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/lib/tools.jar:/Users/Basse/IdeaProjects/Delos/out/production/Delos:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain Main
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:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
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)
... 45 more
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
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 controllers.Controller.handleLogin(Controller.java:24)
... 55 more