1

I encountered this problem while setting the controller in the main class, this is my main class:

package projeto;


import java.io.IOException;


import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import projeto.resources.FilmeOverviewController;

public class MainApp extends Application {

    private Stage primaryStage;
    private BorderPane rootLayout;
    private ObservableList<Filmes> filmeDados = FXCollections.observableArrayList();

    public MainApp() {
        filmeDados.add(new Filmes("testmovie1","Acao","1"));
        filmeDados.add(new Filmes("testmovie2","Aventura","3"));
        filmeDados.add(new Filmes("testmovie3","Acao","2"));
        filmeDados.add(new Filmes("testmovie4","Infantil","4"));
    }
    public ObservableList<Filmes> getfilmeDados(){
        return filmeDados;
    }

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("CineTudo");

        initRootLayout();

        showFilmeOverview();
    }
    public void showFilmeOverview() {       
    try {

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation((MainApp.class.getResource("resources/FilmeOverview.fxml")));
        //----------------
        FilmeOverviewController controller = loader.getController();
        controller.setMainApp(this);
        //----------------
        AnchorPane filmeOverview = (AnchorPane) loader.load();
        rootLayout.setCenter(filmeOverview);
    }catch (IOException e){

        e.printStackTrace();
    }

    }

    public void initRootLayout(){
        try {
            //Carrega o layout root do arquivo fxml
            FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("resources/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();
            Scene cena = new Scene(rootLayout);
            primaryStage.setScene(cena);
            primaryStage.show();

        } catch(IOException e) {
            e.printStackTrace();

       }
      }

public Stage getPrimaryStage() {
            return primaryStage;
        }


public static void main(String[] args) {

    launch(args);
}
}

And this is my controller class:

package projeto.resources;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import projeto.Filmes;
import projeto.MainApp;

public class FilmeOverviewController {

    @FXML
    private TableView<Filmes> filmeTable;
    @FXML
    private TableColumn<Filmes, String> nomeColumn;
    @FXML
    private TableColumn<Filmes, String> categoriaColumn;
    @FXML
    private TableColumn<Filmes, String> salaColumn;

    @FXML
    private Label nomeLabel;
    @FXML
    private Label salaLabel;
    @FXML
    private Label categoriaLabel;
    @FXML
    private Label diretorLabel;
    @FXML
    private Label duracaoLabel;
    @FXML
    private Label protagonistaLabel;
    @FXML
    private Label classificacaoLabel;

    // Reference to the main application.
    private MainApp mainApp;


    public FilmeOverviewController() {
    }


    @FXML
    private void initialize() {
        // Initialize the person table with the two columns.
        nomeColumn.setCellValueFactory(cellData -> cellData.getValue().getNome());
        categoriaColumn.setCellValueFactory(cellData -> cellData.getValue().getCategoria());
        salaColumn.setCellValueFactory(cellData -> cellData.getValue().getSala());
    }


    public void setMainApp(MainApp mainApp) {
        this.mainApp = mainApp;

        // Add observable list data to the table
        filmeTable.setItems(mainApp.getfilmeDados());
    }
}

For some reason it gives me a "java.lang.NullPointerException" while running the method showFilmeOverview(); here's the error:

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$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at projeto.MainApp.showFilmeOverview(MainApp.java:49)
    at projeto.MainApp.start(MainApp.java:40)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
    ... 1 more
Exception running application projeto.MainApp

I'm setting the controller in FilmeOverviewController controller = loader.getController(); and controller.setMainApp(this); why is it returning null?

Eduardo Abreu
  • 155
  • 2
  • 11
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Mạnh Quyết Nguyễn May 26 '18 at 13:20
  • I searched stack overflow and found that exact answer but it didnt fix it, i don't know if i'm doing anything wrong, i just followed a tutotorial – Eduardo Abreu May 26 '18 at 13:23

1 Answers1

1

The FXMLLoader creates the controller as part of the process of loading the FXML file, which happens when you call load(). Since you are trying to call loader.getController() before you call loader.load(), the FXMLLoader hasn't created the controller yet, and getController() returns null.

Simply re-order the code:

public void showFilmeOverview() {       
    try {

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation((MainApp.class.getResource("resources/FilmeOverview.fxml")));
        //----------------
        AnchorPane filmeOverview = (AnchorPane) loader.load();
        FilmeOverviewController controller = loader.getController();
        controller.setMainApp(this);
        //----------------
        rootLayout.setCenter(filmeOverview);
    }catch (IOException e){

        e.printStackTrace();
    }

}
James_D
  • 201,275
  • 16
  • 291
  • 322