I am learning JavaFX with: https://code.makery.ch/library/javafx-tutorial/part1/
package ch.makery.address.controller;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("AddressApp");
initRootLayout();
showPersonOverview();
}
/**
* Initializes the root layout.
*/
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("ch.makery.address.view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Shows the person overview inside the root layout.
*/
public void showPersonOverview() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("ch.makery.address.view/PersonOverview.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
// Set person overview into the center of root layout.
rootLayout.setCenter(personOverview);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the main stage.
* #return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
When I'm trying to load fxml according to author:
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("ch.makery.address.view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
I get such exception:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at ch.makery.address.controller.MainApp.initRootLayout(MainApp.java:40)
at ch.makery.address.controller.MainApp.start(MainApp.java:27)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more
Exception running application ch.makery.address.controller.MainApp
I tried to write path different ways but nothing. So, can anyone help me understand how to handle with paths and loader?
I am working on Netbeans 11 and my project layout:
Related
I have a problem while trying to add a button on an HBox. The error that I got is "Cannot invoke "javafx.scene.layout.HBox.getChildren()" because "this.buttonBox" is null" even though I already have an HBox with fx:id buttonBox. Can someone help me with this?
And I also tried to add a new HBox on the code and then add a button but the button did not show up.
Here is my code:
package com.example.simplemaps;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import java.io.IOException;
import java.util.ArrayList;
public class View {
public static final String INTERFACE_LOCATION = "maps-view.fxml";
private static final Image IMG_BACKGROUND = new Image("com/example/simplemaps/AisleForward.jpg");
private static final Image IMG_FOREGROUND = new Image("com/example/simplemaps/Bunny.png");
//private Controller controller;
#FXML private Label label;
#FXML private ImageView imageBackground;
#FXML private ImageView imageForeground;
#FXML private HBox buttonBox;
public View() {
//controller = new Controller();
}
public void pickUp()
{
// TO DO
}
public void putDown()
{
imageForeground.setImage(IMG_FOREGROUND);
}
public void start()
{
try
{
FXMLLoader fxmlLoader = new FXMLLoader(SimpleMaps.class.getResource(INTERFACE_LOCATION));
Parent anchorPane = fxmlLoader.load();
SimpleMaps.mainStage.setScene(new Scene(anchorPane));
SimpleMaps.mainStage.show();
}
catch(IOException e)
{
e.printStackTrace();
System.exit(1);
}
}
public void addWhereToButtons(String exit) {
//for(String exit : exits) {
Button button = new Button(exit);
HBox.setMargin(button, new Insets(10.0,0.0,10.0,5.0));
buttonBox.getChildren().add(button);
button.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
//controller.selectExitDirection(exit);
}
}
);
//}
}
}
This is the error:
/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home/bin/java --module-path /Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib --add-modules javafx.controls,javafx.fxml -Djava.library.path=/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=51875:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/ulfianidian/Desktop/IPPO/SimpleMaps/target/classes:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx-swt.jar:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx.web.jar:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx.base.jar:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx.fxml.jar:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx.media.jar:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx.swing.jar:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx.controls.jar:/Users/ulfianidian/Desktop/IPPO/javafx-sdk-17.0.0.1/lib/javafx.graphics.jar com.example.simplemaps.SimpleMaps
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.layout.HBox.getChildren()" because "this.buttonBox" is null
at com.example.simplemaps.View.addWhereToButtons(View.java:66)
at com.example.simplemaps.Controller.start(Controller.java:21)
at com.example.simplemaps.SimpleMaps.start(SimpleMaps.java:20)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Exception running application com.example.simplemaps.SimpleMaps
Process finished with exit code 1
You need to cast a new HBox to buttonBox, as such.
HBox buttonBox = new HBox();
I have an issue when trying to modify a JavaFx label, created using SceneBuilder, in an asynchronous manner using runLater().
I can modify it from a callback triggered from a button, but when I try to access to the same element from the runLater method I get an error statting that this element is null.
The error code:
jun 30, 2021 3:42:25 P. M. javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 16 by JavaFX runtime of version 11.0.2
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "javafx.scene.control.Label.setText(String)" because "this.texto" is null
at test.view.GuiApp.lambda$0(GuiApp.java:58)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:831)
My code:
public class GuiApp extends Application{
private Region rootLayout;
private Stage stage;
#FXML private Pane tankPane;
#FXML public Label texto ;
public static GuiApp main;
#Override
public void start(Stage primaryStage) throws IOException {
main = this;
stage = primaryStage;
stage.setTitle("mainStage");
stage.setResizable(false);
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(GuiApp.class.getResource("main.fxml"));
rootLayout = (Region) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
stage.setScene(scene);
stage.show();
Platform.runLater(() -> texto.setText("klfdsfklañsd"));
}
public static void main(String[] args) {
launch(args);
}
#FXML
public void updateRandom()
{
Random r = new Random();
texto.setText(Float.toString(r.nextFloat()));
}
#FXML
private void onReload() {
updateRandom();
}
}
I have tried to reduce it to the simplest, and sorry for my english by the way.
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.NullPointerException: Children: child node is null: parent = VBox#4b0d9981
at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Parent.java:542)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:234)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:103)
at javafx.graphics/javafx.scene.layout.VBox.<init>(VBox.java:251)
at MultiplyApp.start(MultiplyApp.java:34)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Exception running application MultiplyApp
My code is
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javax.swing.*;
public class MultiplyApp extends Application {
private TextArea firstInt;
private TextArea secondInt;
private TextArea resultLabel;
RecursiveMultiply myObj= new RecursiveMultiply();
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
primaryStage.setTitle("Recursive Multiply App");
Label promptLabel= new Label("Enter first positive integer: ");
Label promptLabel2 = new Label("Enter second positive integer: ");
firstInt= new TextArea();
firstInt.setPrefColumnCount(4);
firstInt.setPrefRowCount(2);
secondInt= new TextArea();
secondInt.setPrefColumnCount(4);
secondInt.setPrefRowCount(2);
Button calcButton = new Button("Ok");
HBox hbox= new HBox(10, promptLabel, firstInt, promptLabel2, secondInt);
VBox vbox= new VBox(10, hbox, resultLabel);
Scene initial= new Scene(vbox);
primaryStage.setScene(initial);
primaryStage.show();
calcButton.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
try {
int Gal = Integer.parseInt(firstInt.getText());
int Mil = Integer.parseInt(secondInt.getText());
if ((Gal <= 0) || (Mil <= 0))
throw new NegativeDoubleException();
int result= myObj.recMul(Gal, Mil);
// Display the results.
resultLabel.setText(String.format(result+" is the product"));
}
catch(NumberFormatException | NegativeDoubleException e) {
JOptionPane.showMessageDialog(null,e.getMessage()); //what happens if you dont enter a number
}
}
});
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I don't know if this helps but if I run this code in which i have commented out the setText lines it returns "pointer ok" which suggests that the extCon.setStartValues is pointing to the correct method, although as you pointed out this is another instance of the from and not the one I actually want to populate.
package extrusion;
//import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class ExtrusionController {
private double weight = 16.0;
private double rpm = 30.0;
private double time = 0.5;
#FXML
private TextField timeSecs;
#FXML
private TextField revsPerMin;
#FXML
private TextField sampleWeight;
// #FXML
// void 838383(ActionEvent event) {
//
// }
#FXML
public void setStartValues(){
System.out.println("pointer ok");
// sampleWeight.setText("" + weight);
// revsPerMin.setText("" + rpm);
// timeSecs.setText("" + time);
}
}
package extrusion;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class ExtrusionApp extends Application {
private AnchorPane extrusionForm;
private ExtrusionController extrusionController;
public void start(Stage primaryStage) {
ExtrusionController extCon = new ExtrusionController();
primaryStage.setTitle("ColorMatrix Extrusion");
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("ExtrusionForm.fxml"));
try {
extrusionForm = loader.load();
extrusionController = loader.getController();
extCon.setStartValues();
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(extrusionForm);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Thanks James. Here's the Class with extCon commented out and replaced in with extrusionController.setStartValues() which is still giving a null pointer exception. However if I run the original code I posted but comment out the three setText statements in the ExtrusionController class I no longer get the null pointer exception and the code seems to run ok, obviously not populating the TextFields however.
package extrusion;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class ExtrusionApp extends Application {
private AnchorPane extrusionForm;
private ExtrusionController extrusionController;
public void start(Stage primaryStage) {
//ExtrusionController extCon = new ExtrusionController();
primaryStage.setTitle("ColorMatrix Extrusion");
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("ExtrusionForm.fxml"));
try {
extrusionForm = loader.load();
extrusionController = loader.getController();
extrusionController.setStartValues();
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(extrusionForm);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Exception in Application start method
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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at extrusion.ExtrusionApp.start(ExtrusionApp.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Exception running application extrusion.ExtrusionApp
I'm new to Java and I'm really struggling with a null pointer exception to a TextField. I'm trying to populate the TextFields so when the app opens there is already some data in the fields. I know that I'm pointing to the correct method in the ExtrusionController, as I have tried a System.out.println command in the method and it displays ok. I think the problem might be with the references to the TextFields. I've read dozens of forum posts over the last couple of days but I'm going round in circles.
package extrusion;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class ExtrusionApp extends Application {
private AnchorPane extrusionForm;
private ExtrusionController extrusionController;
public void start(Stage primaryStage) {
ExtrusionController extCon = new ExtrusionController();
primaryStage.setTitle("ColorMatrix Extrusion");
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("ExtrusionForm.fxml"));
try {
extrusionForm = loader.load();
extrusionController = loader.getController();
extCon.setStartValues();
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(extrusionForm);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
package extrusion;
//import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class ExtrusionController {
private double weight = 16.0;
private double rpm = 30.0;
private double time = 0.5;
#FXML
private TextField timeSecs;
#FXML
private TextField revsPerMin;
#FXML
private TextField sampleWeight;
// #FXML
// void 838383(ActionEvent event) {
//
// }
#FXML
public void setStartValues(){
System.out.println();
sampleWeight.setText("" + weight);
revsPerMin.setText("" + rpm);
timeSecs.setText("" + time);
}
}
extCon and extrusionController contain 2 different controller instances. extrusionController is used when loading the fxml and objects from the fxml are injected to this instance.
However you call the setStartValues method for the extCon instance that is not used with a fxml and therefore contains null values in fields.
i write two pages ,one page is called segment,another is crftest.First i stay at the segment page,when i press the button,i want to jump the crftest page.
the error message are here
at view.SegmentController.handlecrftest(SegmentController.java:33
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)
here is my showcrftest function which jump the crftestpage
public void showcrftest(){
// Load crftest overview.
try{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("../view/CRFtest.fxml"));
crftestLayout = (AnchorPane) loader.load();
// Show the scene containing the layout.
Scene scene = new Scene(crftestLayout);
primaryStage.setScene(scene);
primaryStage.show();
// Give the controller access to the main app.
SegmentController controller = loader.getController();
controller.setMain(this);
}catch (IOException e) {
e.printStackTrace();
}
}
here is my controller the trigger function i press the button called handlecrftest()
public class SegmentController {
// Reference to the main application
private Main main;
/**
* Is called by the main application to give a reference back to itself.
*
* #param mainApp
*/
public void setMain(Main main) {
this.main = main;
}
#FXML
private void handlecrftest(){
try{
main.showcrftest();
}catch(Exception e){
e.printStackTrace();
}
}
}