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);
}
Related
I am trying to get the selected text from a textArea in my FXMLDocumentController.java and pass it to a second controller class, HeadingsController.java.
I am running the app in Netbeans and it starts up and loads fine. I get the nullPointerException when I click the Hyperlink with fx:id="h1Link". the null pointerException says its at line 27 of HeadingsController.java which is
String selectedText = fxmlDocC.getSelectedTextfromTextArea();
FXMLDocumentController.java is where the textArea im trying to reference is...
package textareatest1;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
public class FXMLDocumentController {
#FXML public TextArea textArea;
String selectedText = new String();
#FXML private HeadingsController headingsController = new HeadingsController();
public String getSelectedTextfromTextArea(){
selectedText = textArea.getSelectedText();
return selectedText;
}
public void replaceSelectedText(String string){
textArea.replaceSelection(string);
}
#FXML public void initialize() {
headingsController.init(this);
textArea.setWrapText(true);
}
}
AND here is the FXML file for the above controller...
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:id="borderPaneRoot" prefHeight="600.0" prefWidth="1000.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="textareatest1.FXMLDocumentController" >
<top>
</top>
<center>
<TextArea fx:id="textArea" />
</center>
<left>
<GridPane id="gridpaneLeft" fx:id="gridpaneLeft" hgap="10.0" prefHeight="200.0" prefWidth="320.0" vgap="10.0" >
<Accordion fx:id="leftAccordion" prefHeight="250.0" prefWidth="680.0">
<panes>
<TitledPane fx:id="headingsTitlePane" animated="true" text="Headings">
<content>
<Pane>
<children>
<fx:include fx:id="fxmlheadings" source="FXMLHeadings.fxml"/>
</children>
</Pane>
</content>
</TitledPane>
</panes>
</Accordion>
</GridPane>
</left>
</BorderPane>
Here is my second controller, HeadingsControllerr.java .....
package textareatest1;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Hyperlink;
public class HeadingsController {
#FXML private FXMLDocumentController fxmlDocC;
String[] headings = {"h1. ","h2. ","h3. ","h4. ","h5. ","h6. "};
#FXML public Hyperlink h1Link;
#FXML
private void handleH1LinkAction(ActionEvent event) {
String selectedText = fxmlDocC.getSelectedTextfromTextArea();
System.out.println(selectedText);
// System.out.println("test");
}
public void init(FXMLDocumentController fxmlDocumentController) {
fxmlDocC = fxmlDocumentController;
}
}
AND here is the FXML file for the HeadingsController.java...
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="textareatest1.HeadingsController" fx:id="fxmlheadings">
<Hyperlink id="links" fx:id="h1Link" text="h1. Biggest heading" layoutX="5.0" layoutY="3.0" onAction="#handleH1LinkAction" />
</AnchorPane>
Here is the stacktrace with the nullPointerException I am getting which I believe is because fxmlDocC is null... I think I need to create an instance of FXMLDocumentController properly?
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.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 javafx.scene.control.Hyperlink.fire(Hyperlink.java:153)
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.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: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$353(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$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)
... 56 more
Caused by: java.lang.NullPointerException
at textareatest1.HeadingsController.handleH1LinkAction(HeadingsController.java:27)
... 66 more
I am trying to highlight text in the TextArea through the UI, and have it print in the console when I click the hyperlink with fx:id="h1Link".
This issue is not solved by creating an instance of FXMLDocumentController, but by making sure the controller you pass the instance to is the one used with the included fxml; the one you create with new HeadingsController() is not this instance.
Instead the fact that FXMLLoader injects the controller for the included element to a field with the name you get, if you append Controller to the fx:id of the <fx:include> element:
public class FXMLDocumentController {
#FXML public TextArea textArea;
String selectedText = ""; // why is this field necessary???
// controller for FXMLHeadings.fxml is automatically injected here
#FXML private HeadingsController fxmlheadingsController;
public String getSelectedTextfromTextArea(){
selectedText = textArea.getSelectedText();
return selectedText;
}
public void replaceSelectedText(String string){
textArea.replaceSelection(string);
}
#FXML public void initialize() {
fxmlheadingsController.init(this);
textArea.setWrapText(true);
}
}
i had a problem with this code here, just wanna make a small calculator:
package application;
import javafx.event.ActionEvent;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.stage.Stage;
import javafx.scene.Scene;
//import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
//BorderPane root = new BorderPane();
Parent root = FXMLLoader.load(getClass().getResource("Root.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
#FXML
private TextField textField, textField2;
#FXML
private Label label;
#FXML
protected void onClick(ActionEvent event) {
textField.getText();
textField2.getText();
textField = Integer.parseInt(textField);
textField2 = Integer.parseInt(textField2);
label = textField + textField2;
label = Integer.toString(label);
label.setText(label.getText());
}
}
so i got a problem when i tryed to calcul 1 and 1... I cant copy the out put bcs its too long, try this code on your pc to get the output, and i think the problem is the Integer.
Edit:output here:
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 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: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(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: java.lang.Error: Unresolved compilation problems:
The method parseInt(String) in the type Integer is not applicable for the arguments (TextField)
The method parseInt(String) in the type Integer is not applicable for the arguments (TextField)
The operator + is undefined for the argument type(s)
javafx.scene.control.TextField, javafx.scene.control.TextField
The method toString(int) in the type Integer is not applicable for the arguments (Label)
at application.Main.onClick(Main.java:44)
... 58 more
ok so here the the outputs does that helps more?
Edit2:FXML here:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="278.0" prefWidth="473.0"
xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.Main">
<children>
<TextField fx:id="textField" layoutX="24.0" layoutY="54.0"
prefHeight="42.0" prefWidth="126.0" />
<TextField fx:id="textField2" layoutX="174.0" layoutY="54.0"
prefHeight="42.0" prefWidth="126.0" />
<Label fx:id="label" layoutX="323.0" layoutY="54.0" prefHeight="42.0"
prefWidth="126.0" text="Label" />
<Button layoutX="174.0" layoutY="162.0" mnemonicParsing="false"
onAction="#onClick" prefHeight="42.0" prefWidth="126.0" text="Sum" />
</children>
</AnchorPane>
so here you can run the program.
This should help you on your way to understanding what is going on, good luck:
protected void onClick(ActionEvent event) {
// getText() returns type String
String text1 = textField.getText();
String text2 = textField2.getText();
// parse() returns type int or Integer
int int1 = Integer.parseInt(text1);
int int2 = Integer.parseInt(text2);
// using Integer here so we can use toString() next
Integer result = int1 + int2;
// setText() takes input of type String
label.setText(result.toString());
}
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" />
...
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();
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.