java - 设置 Controller 时出现异常

标签 java javafx

我在主类中设置 Controller 时遇到了这个问题,这是我的主类:

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);
}
}
<小时/>

这是我的 Controller 类:

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());
    }
}

出于某种原因,它在运行方法 showFilmeOverview() 时给了我一个“java.lang.NullPointerException”;这是错误:

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

我正在 FilmeOverviewController controller = loader.getController(); 中设置 Controller controller.setMainApp(this); 为什么它返回 null?

最佳答案

FXMLLoader 创建 Controller 作为加载 FXML 文件过程的一部分,该过程在您调用 load() 时发生。由于您尝试在调用 loader.load() 之前调用 loader.getController(),因此 FXMLLoader尚未创建 Controller ,getController() 返回 null。

只需重新排序代码:

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();
    }

}

关于java - 设置 Controller 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50543171/

相关文章:

java - RAM 过度支出 - JavaFX

java - TimesTen 11.2.2 Hibernate 4 方言

javascript - 如何检查突出显示文本的 if else 条件?

Java inputstreamreader 解析为 int 然后反转数组并打印为格式化字符串

Java fx 打印文本

multithreading - Javafx 几个 GUI 线程 - 进度指示器

css - 使默认按钮看起来像普通按钮

java - Javafx 的新阶段即将到来

Java,从单独的 JSON 值到字符串的日期

java - 编写/实现 API : testability vs information hiding