gpt4 book ai didi

java - 设置 Controller 时出现异常

转载 作者:行者123 更新时间:2023-11-30 06:09:43 25 4
gpt4 key购买 nike

我在主类中设置 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com