I have a project in which I ask a user what type of electronic devices they use to tell them how many solar panels and batteries they need.
To collect information, I use a gridPane with three columns, in which there are TextArea, and a variable amount of rows. I need to collect the information in the second and third row of the gridPane.
I use the following method which I found on another question asked here.
private void calculer(ActionEvent event)
{
double ampApp = 0;
double heureApp = 0;
double wattTotal = 0;
double nbJourAuto;
double heureSoleil;
int nbPanneau;
int nbBatterie;
String stringTemp;
for(int i =1; i < rangee; i++)
{
stringTemp = getNodeByRowColumnIndex( i, 1, gridApp).toString();
ampApp += parseDouble(stringTemp);
}
for(int i =1; i < rangee; i++)
{
stringTemp = getNodeByRowColumnIndex(i, 2, gridApp).toString();
heureApp += parseDouble(stringTemp);
}
(it's only the first part of the method, I know there is a missing "}")
And here is the getNodeByRowColumnIndex code:
public Node getNodeByRowColumnIndex( final int row, final int column, GridPane gridPane) {
Node result = null;
ObservableList<Node> childrens = gridPane.getChildren();
for(Node node : childrens)
{
if(gridPane.getRowIndex(node) == row && gridPane.getColumnIndex(node) == column)
{
result = node;
break;
}
}
return result;
}
I tried this method, but keep getting java.lang.NullPointerException
Does anyone knows why and how to solve this issues?
Edit :
As requested here's the stack trace
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)
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)
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 systemesolaire.FXMLDocumentController.getNodeByRowColumnIndex(FXMLDocumentController.java:111)
at systemesolaire.FXMLDocumentController.calculer(FXMLDocumentController.java:81)
... 58 more
and here's how I intialize my children (I used SceneBuilder so here is the FXML file)
<content>
<GridPane fx:id="gridApp" prefHeight="141.0" prefWidth="452.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="226.0" minWidth="10.0" prefWidth="226.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="146.0" minWidth="10.0" prefWidth="123.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="113.0" minWidth="10.0" prefWidth="103.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="40.0" prefHeight="40.0" vgrow="NEVER" />
<RowConstraints minHeight="40.0" prefHeight="40.0" vgrow="NEVER" />
<RowConstraints minHeight="40.0" prefHeight="40.0" vgrow="NEVER" />
</rowConstraints>
<children>
<Label text=" Nom de l'appareil">
<font>
<Font name="Anke" size="18.0" />
</font>
</Label>
<Label text="Ampérage" GridPane.columnIndex="1">
<font>
<Font name="Anke" size="14.0" />
</font>
</Label>
<Label text="Heures utilisation" GridPane.columnIndex="2">
<font>
<Font name="Anke" size="13.0" />
</font>
</Label>
<TextArea prefHeight="200.0" prefWidth="200.0" promptText="Nom" GridPane.rowIndex="1" />
<TextArea prefHeight="200.0" prefWidth="200.0" promptText="Amp" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextArea prefHeight="200.0" prefWidth="200.0" promptText="Heure" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<TextArea prefHeight="200.0" prefWidth="200.0" promptText="Nom" GridPane.rowIndex="2" />
<TextArea prefHeight="200.0" prefWidth="200.0" promptText="Amp" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextArea prefHeight="200.0" prefWidth="200.0" promptText="Heure" GridPane.columnIndex="2" GridPane.rowIndex="2" />
</children>
</GridPane>
</content>
And here is How I add a row to my gridPane
#FXML
private void ajouteRangee(ActionEvent event)
{
TextArea nom = new TextArea();
TextArea amp = new TextArea();
TextArea heure = new TextArea();
nom.setPromptText("Nom");
amp.setPromptText("Amp");
heure.setPromptText("heure");
gridApp.addRow(rangee, nom, amp, heure);
gridApp.layout();
rangee++;
}
I got the answer with the help of my teacher, if anybody has the same issue, here's how I solved it.
The NullPointerException came for the getColumnIndex, the solution bypasses the issue, but I don't know why I was getting this exception in the first place.
Since the amount of rows and columns is known, there is no need to search every node for the right one. Also, since the number of children per row is the same, there is a logical way to get a specific children. Here's how :
public TextArea getNodeByRowColumnIndex( final int row, final int column, GridPane gridPane)
{
ObservableList<Node> childrens = gridPane.getChildren();
TextArea result = (TextArea) childrens.get((row*3)+column);
return result;
}
The ObservableList gets children from left to right row by row, so you can get to a specific object in the list with this formula (((rowIndex + 1) * the amount of object per row) + on which column is your object)
And then you can get the TextArea text by doing:
stringTemp = getNodeByRowColumnIndex( i, 1, gridApp).getText();
Related
I am creating a simple planning JavaFX 8 program using Scene Builder. My program consists of multiple controllers going from one screen to the other as the user advances the program.
I have created a custom component using SceneBuilder. It is just a strip consisting of a label, 2 sliders, and a checkbox in a GridPane. I have a corresponding controller with just the getters and setters for everything inside.
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<fx:root fx:id="pane" prefHeight="53.0" prefWidth="1495.0" type="GridPane" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Problem">
<children>
<Label fx:id="name" text="subProblem" GridPane.halignment="CENTER" />
<Slider fx:id="time" min="1.0" value="1.0" GridPane.columnIndex="1" GridPane.halignment="CENTER">
<GridPane.margin>
<Insets left="10.0" right="10.0" />
</GridPane.margin>
</Slider>
<Slider fx:id="priority" min="1.0" value="1.0" GridPane.columnIndex="2" GridPane.halignment="CENTER">
<GridPane.margin>
<Insets left="10.0" right="10.0" />
</GridPane.margin>
</Slider>
<CheckBox fx:id="continuous" mnemonicParsing="false" styleClass="no-label-checkbox" GridPane.columnIndex="3" GridPane.halignment="CENTER" />
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="1113.0" minWidth="99.0" prefWidth="350.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="526.0" minWidth="10.0" prefWidth="526.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="526.0" minWidth="10.0" prefWidth="526.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="134.0" minWidth="0.0" prefWidth="134.0" />
</columnConstraints>
<padding>
<Insets bottom="16.0" top="16.0" />
</padding>
<rowConstraints>
<RowConstraints />
</rowConstraints>
</fx:root>
Controller:
package application;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
public class Problem {
// fields
#FXML private Label name = new Label();
#FXML private Slider time = new Slider();
#FXML private Slider priority = new Slider();
#FXML private CheckBox continuous = new CheckBox();
// constructor
public Problem(String name) {
super();
this.name.setText(name);
}
// getters and setters
public Label getName() {
return name;
}
public void setName(Label name) {
this.name = name;
}
public Slider getTime() {
return time;
}
public void setTime(Slider time) {
this.time = time;
}
public Slider getPriority() {
return priority;
}
public void setPriority(Slider priority) {
this.priority = priority;
}
public CheckBox getContinuous() {
return continuous;
}
public void setContinuous(CheckBox continuous) {
this.continuous = continuous;
}
// utility methods
public int getWeighting() {
return (int) Math.floor(this.time.getValue() * this.priority.getValue());
}
}
I created an ObservableList of this Problem object in one controller:
private ObservableList<Problem> problemList = FXCollections.observableArrayList();
//...
// to the next screen
public void toWeighting(ActionEvent e) throws IOException {
saveProblems();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("Weighting.fxml"));
Parent weightingParent = loader.load();
Scene weightingScene = new Scene(weightingParent);
// access the breakdown controller
WeightingController controller = loader.getController();
controller.initData(saveFile, problemList);
Stage window = (Stage)((Node)e.getSource()).getScene().getWindow();
window.setScene(weightingScene);
window.show();
}
This all works fine. I now passed an ObservableList of my Problem object to the next controller. My next controller has a VBox in it. I would like to add every element of the ObservableList into the VBox. I looked up ways to do this, but nothing would work.
This is the problem area:
#FXML private VBox listBox = new VBox();
public void initData(File saveFile, ObservableList<Problem> problemList) throws IOException {
this.saveFile = saveFile;
FXMLLoader loader = new FXMLLoader(getClass().getResource("Problem.fxml"));
for (Problem p : problemList) {
loader.setRoot(p);
listBox.getChildren().add(loader.load());
}
}
It gave me this error:
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:3760)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3488)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1765)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2497)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
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$2(GlassViewEventHandler.java:434)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:411)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:941)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:185)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: javafx.fxml.LoadException:
/D:/Workspace/eclipse/Problem%20Planner%20JavaFX/bin/application/Problem.fxml:11
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932)
at javafx.fxml.FXMLLoader$RootElement.processAttribute(FXMLLoader.java:1290)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at application.WeightingController.initData(WeightingController.java:27)
at application.BreakdownController.toWeighting(BreakdownController.java:109)
... 58 more
Caused by: java.lang.InstantiationException: application.Problem
at java.lang.Class.newInstance(Unknown Source)
at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
... 67 more
Caused by: java.lang.NoSuchMethodException: application.Problem.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
... 70 more
I tried other ways, like extending the Problem class with GridPane, making a getter for the GridPane, but either I get an error or nothing shows up. Please help, thanks!
EDIT: I have this weird solution for now.
I added an initialize method and an empty constructor to Problem, extended GridPane, and edited the trouble method:
FXMLLoader loader = new FXMLLoader(getClass().getResource("Problem.fxml"));
for (Problem p : problemList) {
String problemName = p.getName().getText();
loader.setController(p);
loader.setRoot(p);
listBox.getChildren().add(loader.load());
p.getName().setText(problemName);
}
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;
}
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" />
...
My buttons were working well before I added the table (TableView) functionality to my code, but now an error occurs once I click on the same buttons. The error disappears and the buttons works if I comment out this lines of code in RoomsController:
intRoomIDColVR.setCellValueFactory(new PropertyValueFactory<>("roomID"));
vachRoomTypeIDColVR.setCellValueFactory(new PropertyValueFactory<>("roomTypeID"));
roomTableViewVR.setItems(olRoomData);
The FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.codetech.hotelmis.controller.RoomsController">
<children>
<BorderPane prefHeight="338.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<center>
<Accordion BorderPane.alignment="CENTER">
<panes>
<TitledPane fx:id="accVacant" animated="false" text="Vacant Rooms">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<BorderPane prefHeight="324.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<center>
<TableView fx:id="roomTableViewVR" BorderPane.alignment="CENTER">
<columns>
<TableColumn fx:id="intRoomIDColVR" prefWidth="75.0" text="Room ID" />
<TableColumn fx:id="vachRoomTypeIDColVR" prefWidth="115.00000762939453" text="Room Type" />
<TableColumn fx:id="reservedColVR" prefWidth="94.0" text="Reserved?" />
<TableColumn fx:id="resCustomerColVR" prefWidth="95.0" text="Customer " />
</columns>
</TableView>
</center>
<top>
<HBox>
<children>
<Button fx:id="btnCheckInPopUp" mnemonicParsing="false" text="Check In" BorderPane.alignment="CENTER" />
</children>
</HBox>
</top>
</BorderPane>
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane fx:id="accOccupied" animated="false" text="Occupied Rooms">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<BorderPane prefHeight="324.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
<HBox>
<children>
<Button fx:id="btnTabCheckOut" mnemonicParsing="false" text="Check Out" BorderPane.alignment="CENTER" />
</children>
</HBox>
</top>
<center>
<TableView BorderPane.alignment="CENTER">
<columns>
<TableColumn fx:id="intRoomIDColOR" prefWidth="75.0" text="Room ID" />
<TableColumn fx:id="vachRoomTypeIDColOR" prefWidth="115.00000762939453" text="Room Type" />
<TableColumn fx:id="occupantColOR" prefWidth="83.0" text="Occupant" />
<TableColumn fx:id="checkInDateColOR" prefWidth="121.0" text="Check In Date" />
<TableColumn fx:id="checkOutDateColOR" prefWidth="181.99996948242188" text="Expected Check Out Date" />
</columns>
</TableView>
</center>
</BorderPane>
</children>
</AnchorPane>
</content>
</TitledPane>
</panes>
</Accordion>
</center>
<top>
<HBox prefHeight="24.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="btnNewRoom" mnemonicParsing="false" text="New Room" />
<Button fx:id="btnEditRoom" mnemonicParsing="false" text="Edit Room" />
<Button fx:id="btnNewRoomType" mnemonicParsing="false" text="New Room Type" />
<Button fx:id="btnEditRoomType" mnemonicParsing="false" text="Edit Room Type" />
<Button fx:id="btnGuestLog" mnemonicParsing="false" text="Guest Log" />
</children>
</HBox>
</top>
</BorderPane>
</children>
</AnchorPane>
The Controller Class:
public class RoomsController implements Initializable {
#FXML
private TableView<RoomData> roomTableViewVR;
#FXML
private TableColumn<RoomData, Integer> intRoomIDColVR;
#FXML
private Button btnCheckInPopUp;
#FXML
private TableColumn<RoomData, String> vachRoomTypeIDColVR;
//Declare ObservableList
private ObservableList<RoomData> olRoomData;
private ActionEvent event;
private static Connection con;
private static ResultSet rs;
private static java.sql.Statement stmt;
private static String sql;
JavaToMySQL javaToMySQL = new JavaToMySQL();
public RoomsController() {
try {
con = DatabaseConnection.getConnected();
stmt = con.createStatement();
olRoomData = FXCollections.observableArrayList();
rs = con.createStatement().executeQuery("SELECT * FROM h_r_rooms");
while (rs.next()){
olRoomData.add(new RoomData(rs.getInt("intRoomID"),rs.getString("vachRoomTypeID")));
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error on Building Data");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
#Override
public void initialize(URL location, ResourceBundle resources) {
listeners();
intRoomIDColVR.setCellValueFactory(new PropertyValueFactory<>("roomID"));
vachRoomTypeIDColVR.setCellValueFactory(new PropertyValueFactory<>("roomTypeID"));
roomTableViewVR.setItems(olRoomData);
}
void listeners(){
btnCheckInPopUp.setOnAction((event -> {
System.out.println("I'm supposed to pop out a Check In window when you click me!");
Stage stage;
if (event.getSource()== btnCheckInPopUp){
stage = new Stage();
try {
Parent root = FXMLLoader.load(getClass().getResource("../view/popups/check_in.fxml"));
System.out.println("I work!");
stage.setScene(new Scene(root));
stage.setTitle("Check Customer In");
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(btnCheckInPopUp.getScene().getWindow());
stage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
}));
}
}
When I click on the Check In(btnCheckInPopUp) the following error appears:
The Connection is open!
javafx.fxml.LoadException:
/home/mush/Programming/Dev/InteliJProjects/Hotel_MIS/out/production/Hotel_MIS/com/codetech/hotelmis/view/popups/check_in.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2583)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
at com.codetech.hotelmis.controller.RoomsController.lambda$listeners$0(RoomsController.java:87)
at com.codetech.hotelmis.controller.RoomsController$$Lambda$104/1394020985.handle(Unknown Source)
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.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:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8390)
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.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$MouseHandler.process(Scene.java:3758)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$252/507580015.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:927)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$43/1166661505.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at com.codetech.hotelmis.controller.RoomsController.initialize(RoomsController.java:74)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
... 70 more
I've really tried to figure out why but to no avail. I'd really appreciate the help and I will provide any more details concerning the code required to solve the issue.
The problem here
seems quite similar to mine.
It appears you are using the same controller class for two different FXML files. Don't do this: use a different controller for each FXML.
When the FXMLLoader's load() method is called, it does the following (somewhat simplified description):
Reads the FXML file
If the FXML file specifies a controller class, it creates a new instance of the controller class by calling its no-argument constructor
Creates an object hierarchy based on the FXML elements
Sets event handlers on the elements, mapping to the controller class instance created in step 1
For elements with an fx:id attribute, it injects the corresponding objects into any matching #FXML-annotated references in the controller instance
Calls initialize() on the controller instance, if such a method exists.
Returns the object corresponding to the root element of the FXML file.
In your case, you have two FXML files specifying the same controller class. When the "main" FXML file is loaded, the process above happens. A first instance of RoomsController is created, its #FXML-annotated fields matching fx:ids are injected (including intRoomIDColVR and others), event handlers are registered, and the initialize() method is called. All is fine at this point.
When the btnCheckInPopup button is pressed, its onAction handler is invoked. This causes check_in.fxml to be loaded. Since this specifies the same controller class, a second instance of that class is created. #FXML-annotated fields corresponding to elements of check_in.fxml with matching fx:ids are injected. However, note that, since check_in.fxml doesn't define any element with fx_id="intRoomIDColVR", intRoomIDColVR is not initialized for this second controller instance. The initialize() method is then invoked on this second controller instance, and since intRoomIDColVR is not initialized, this causes a NullPointerException at the line
intRoomIDColVR.setCellValueFactory(new PropertyValueFactory<>("roomID"));
This is a pretty good example of exactly why each FXML should have its own controller class. It gets very hard to keep track of which fields are initialized and which are not at any given time, and for any specific instance of the class, if you "share" controller classes like this.
I am new to Javafx. I am learning Javafx by developing a simple application. I am trying go get value from TextField in javaFXML. I got java.lang.reflect.InvocationTargetException.
Javafx File.
public class Manoj extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
FXMLDocument.fxml
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="manoj.FXMLDocumentController">
<children>
<AnchorPane layoutX="142.0" layoutY="36.0" prefHeight="361.0" prefWidth="360.0">
<children>
<Label fx:id="result" alignment="CENTER" contentDisplay="CENTER" layoutX="61.0" layoutY="289.0" opacity="0.51" prefHeight="77.0" prefWidth="169.0" text="Result Of Addition">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="26.0" layoutY="108.0" prefHeight="32.0" prefWidth="90.0" text="Number#1 ">
<font>
<Font size="14.0" />
</font>
</Label>
<Label alignment="TOP_CENTER" contentDisplay="CENTER" text="First Application" textAlignment="CENTER">
<font>
<Font name="Cambria" size="50.0" />
</font>
</Label>
<Label layoutX="26.0" layoutY="155.0" prefHeight="32.0" prefWidth="90.0" text="Number#2">
<font>
<Font size="14.0" />
</font>
</Label>
<Button fx:id="button" alignment="CENTER" contentDisplay="CENTER" layoutX="83.0" layoutY="216.0" onAction="#addNumber" prefHeight="44.0" prefWidth="125.0" text="Add Number">
<font>
<Font name="Cambria" size="14.0" />
</font>
</Button>
<TextField fx:id="textone" layoutX="141.0" layoutY="108.0" prefHeight="32.0" prefWidth="179.0" />
<TextField fx:id="texttwo" layoutX="141.0" layoutY="155.0" prefHeight="32.0" prefWidth="179.0" />
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" />
</padding>
</AnchorPane>
</children>
<padding>
<Insets top="20.0" />
</padding>
</AnchorPane>
FXMLDocumentController.java
public class FXMLDocumentController implements Initializable {
#FXML
private Label result;
private TextField textone;
public FXMLDocumentController() {
}
#FXML
private void addNumber(ActionEvent event) {
System.out.println("Clicked. !");
String str = textone.getText();
result.setText(str);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
I am developing using netbeans with scene builder. When I run this project and click Add Number button, I got an exception.
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1768)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1651)
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:204)
at javafx.scene.Node.fireEvent(Node.java:8175)
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:204)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3746)
at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)
at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
at com.sun.glass.ui.View.notifyMouse(View.java:898)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:745)
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:483)
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:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1763)
... 47 more
Caused by: java.lang.NullPointerException
at manoj.FXMLDocumentController.addNumber(FXMLDocumentController.java:33)
... 57 more
Please help me to get out of this exception. Thanks in advance.
Thanks for your comment Jose Pereda.
Just a simple careless mistake.
As Jose Pereda said, I added #FXML annotation for textone and it worked fine.
package net.remark.abs.invoice;
import java.util.Date;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import net.remark.abs_bos.Invoice;
import net.rojer.core.db.DataBase;
import net.rojer.javafx.tool.Tools;
public class FrameInvoice extends AnchorPane{
#FXML
private TextField fldInvoiceNumber;
#FXML
private Label lblPrice;
public FrameInvoice(){
Tools.innitiateFxml(this);
}
#FXML
private void onSave (ActionEvent event){
Invoice invo = new Invoice();
invo.setDate(new Date());
invo.setTotal(0);
invo.setStat((true));
DataBase.begin();
DataBase.em().persist(invo);
DataBase.commit();
System.out.println("true");
}
}
I have seen many posts regarding this error and many of them get failed.For me it was the same. try to make the fxml file again and let the IDE to create the controller and css. keep the default controller and css names as it is and attach newly created '.fxml' file to the code. For me It worked.copy the content from the scene builder ,but not by the xml code.