0

I have created a new JavaFx project. In the project I have a Main class, a Controller class and a design file.

When I click on "New products" I want to load a new fxml file named new_product.fxml.

enter image description here

However I get a error when I click the "New product" button:

Caused by: java.lang.NullPointerException
    at sample.Main.changeScene(Main.java:33)
    at sample.Controller.buttonNewProductOnMouseClicked(Controller.java:21)

In Main.java line 33 is the primaryStage. Looks like my controller can't access it?

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {

    Stage primaryStage;

    @Override
    public void start(Stage primaryStage) throws Exception{
        this.primaryStage = primaryStage;
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        Scene primaryScene = new Scene(root, 300, 275);

        primaryStage.setScene(primaryScene);
        primaryStage.show();

    }
    public void changeScene(String fxml){
        Parent pane = null;
        try {
            pane = FXMLLoader.load(getClass().getResource(fxml));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene( pane );
        primaryStage.setScene(scene);
    }

    public static void main(String[] args) {
        launch(args);
    }
}

My design file

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <top>
      <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <Button fx:id="buttonProducts" mnemonicParsing="false" onMouseClicked="#buttonProductsOnMouseClicked" text="Products" />
            <Button fx:id="buttonNewProduct" mnemonicParsing="false" onMouseClicked="#buttonNewProductOnMouseClicked" text="New products">
               <HBox.margin>
                  <Insets left="5.0" />
               </HBox.margin></Button>
         </children>
      </HBox>
   </top>
   <center>
      <Label text="Label" BorderPane.alignment="CENTER" />
   </center>
</BorderPane>

The Controller class for the design file

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.Button;

public class Controller {

    @FXML
    private Button buttonProducts;

    @FXML
    private Button buttonNewProduct;

    public void buttonProductsOnMouseClicked(javafx.scene.input.MouseEvent mouseEvent) {
        buttonProducts.setText("You have clicked on me!");
    }
    public void buttonNewProductOnMouseClicked(javafx.scene.input.MouseEvent mouseEvent) {
        buttonNewProduct.setText("You have clicked on me!");

        Main mainclass = new Main();
        mainclass.changeScene("new_product/new_product.fxml");
    }
}
fabian
  • 80,457
  • 12
  • 86
  • 114
Sigma
  • 45
  • 6
  • 1
    You are creating a new instance of `Main`, one on which no one calls `start`, so the `primaryStage` is never set. See also [Can application class be the controller class](https://stackoverflow.com/questions/33303167/javafx-can-application-class-be-the-controller-class), although it is *not exactly* the same issue. – Itai May 17 '18 at 10:42
  • Just move the `changeScene()` method to the controller. There is no need to retain a reference to the `primaryStage`: you can get a reference to the stage containing the button with `Stage stage = (Stage) buttonNewProduct.getScene().getWindow();`. Also, don't use `onMouseClicked` with a button: use `onAction`. – James_D May 17 '18 at 11:11
  • But if you want to access the `Main` class, use one of the approaches described here: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian May 17 '18 at 13:37

0 Answers0