设置控制器时的例外情况

时间:2018-05-26 13:16:17

标签: java javafx

我在主类中设置控制器时遇到了这个问题,这是我的主要类:

videoview

这是我的控制器类:

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

由于某种原因,它给了我一个&#34; java.lang.NullPointerException&#34;在运行方法showFilmeOverview();这是错误:

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

我在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();为什么它返回null?

1 个答案:

答案 0 :(得分:1)

FXMLLoader在加载FXML文件的过程中创建控制器,这在您调用load()时会发生。由于您尝试在之前调用loader.getController() ,因此调用loader.load()FXMLLoader尚未创建控制器,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();
    }

}