i just started JavaFX programming with Netbeans 11.0 on Windows 10 platform. when i was trying to use FXMLcontroller i faced this problem. details of my project are below:
there is a FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.*?>
<AnchorPane id="root" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nikman.SubCreatorController">
<children>
<FlowPane id="toopi">
<children>
<Label id="somelbl" text="hi!"/>
</children>
</FlowPane>
</children>
</AnchorPane>
And it's controller class:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package nikman;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
/**
* FXML Controller class
*
* @author Hace
*/
public class SubCreatorController implements Initializable {
/**
* Initializes the controller class.
* @param url
*/
@FXML
private FlowPane toopi;
@FXML
private Label somelbl;
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
somelbl.setText("Hola!");
}
}
By running the project using netbeans, following compiler log is given to me. but if somelbl.setText("Hola!");
is removed from controller class, project runs with no problem:
Compiler Log:
jfx-project-run:
[echo] Executing E:\Nikazma_Management\Nikman\dist\run195265712\Nikman.jar using platform C:\Program Files\Java\jdk1.8.0_231/bin/java
[java] Exception in Application start method
[java] java.lang.reflect.InvocationTargetException
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[java] at java.lang.reflect.Method.invoke(Method.java:498)
[java] at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
[java] at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[java] at java.lang.reflect.Method.invoke(Method.java:498)
[java] at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
[java] Caused by: java.lang.RuntimeException: Exception in Application start method
[java] at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
[java] at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
[java] at java.lang.Thread.run(Thread.java:748)
[java] Caused by: javafx.fxml.LoadException:
[java] file:/E:/Nikazma_Management/Nikman/dist/run195265712/Nikman.jar!/nikman/SubCreator.fxml
[java]
[java] at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
[java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
[java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
[java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
[java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
[java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
[java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
[java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
[java] at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
[java] at nikman.Nikman.start(Nikman.java:104)
[java] at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
[java] at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
[java] at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
[java] at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
[java] at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
[java] at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
[java] ... 1 more
[java] Caused by: java.lang.NullPointerException
[java] at nikman.SubCreatorController.initialize(SubCreatorController.java:37)
[java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
[java] ... 17 more
[java] Exception running application nikman.Nikman
if needed, there is my Application class for this project too:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package nikman;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.paint.Color;
import javafx.scene.layout.CornerRadii;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
/**
*
* @author Hace
*/
public class Nikman extends Application {
@Override
public void start(Stage primaryStage) throws FileNotFoundException, IOException {
//Defining theme related constants
Background foreG = new Background(new BackgroundFill(Color.rgb(202, 209, 224), CornerRadii.EMPTY, Insets.EMPTY));
Background backG = new Background(new BackgroundFill(Color.rgb(180, 190, 207), CornerRadii.EMPTY, Insets.EMPTY));
/*
Line 39 to 52 specify main UI parts
*/
//defining panels to be used in UI
AnchorPane root = new AnchorPane(); // the panel that contains all childs
AnchorPane titleBar = new AnchorPane();
VBox menu = new VBox();
StackPane continer = new StackPane(); // the panel that contains forms
//fitting title bar in root
AnchorPane.setTopAnchor(titleBar, 0.0);
AnchorPane.setLeftAnchor(titleBar, 0.0);
AnchorPane.setRightAnchor(titleBar, 0.0);
titleBar.setPrefHeight(80.0);
Line tBLine = new Line(0,titleBar.getPrefHeight(),50000.0,titleBar.getPrefHeight());
//fitting menu in root
AnchorPane.setTopAnchor(menu, titleBar.getPrefHeight());
AnchorPane.setLeftAnchor(menu, 0.0);
AnchorPane.setBottomAnchor(menu, 0.0);
menu.setPrefWidth(220.0);
menu.setPadding(new Insets(5.0,5.0,5.0,5.0));
menu.setSpacing(5.0);
menu.fillWidthProperty();
//fitting continer in root
AnchorPane.setTopAnchor(continer, titleBar.getPrefHeight());
AnchorPane.setLeftAnchor(continer, menu.getPrefWidth());
AnchorPane.setRightAnchor(continer, 0.0);
AnchorPane.setBottomAnchor(continer, 0.0);
//finishing UI compartment
root.getChildren().addAll(titleBar, menu, continer, tBLine);
titleBar.setBackground(foreG);
menu.setBackground(foreG);
continer.setBackground(backG);
//adding main logo on titleBar
Image mLogo = new Image(new FileInputStream("Assets/test.png"));
ImageView logoVi = new ImageView(mLogo);
AnchorPane.setRightAnchor(logoVi,5.0);
AnchorPane.setTopAnchor(logoVi,5.0);
AnchorPane.setBottomAnchor(logoVi,10.0);
logoVi.setFitHeight(65);
logoVi.setFitWidth(300);
logoVi.preserveRatioProperty();
titleBar.getChildren().add(logoVi);
//Adding menu buttons
Button editor_btn = new Button("File Editor");
editor_btn.setPrefWidth(menu.getPrefWidth());
Button index_btn = new Button("File Index");
index_btn.setPrefWidth(menu.getPrefWidth());
Button about_btn = new Button("About Project");
about_btn.setPrefWidth(menu.getPrefWidth());
menu.getChildren().addAll(editor_btn,index_btn,about_btn);
//Adding SubCreator Pane
FXMLLoader warehouse = new FXMLLoader();
Pane sub_Create = warehouse.load(getClass().getResource("SubCreator.fxml"));
continer.getChildren().add(sub_Create);
//Defining last properties of main window
Scene scene = new Scene(root, 800, 600);
primaryStage.setTitle("someProject");
primaryStage.setScene(scene);
primaryStage.setMinWidth(800);
primaryStage.setMinHeight(600);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}