javaFX在启动控制器时获取空指针

时间:2016-04-20 13:04:08

标签: java javafx

我在初始化表时遇到空指针异常。

此行controller.setUserDetailsList(userDetailsList)正在抛出错误 即使我在userDetailsList中添加数据。 任何想法如何解决这个问题?感谢..

public class Main extends Application {
    private Stage primaryStage;
    private ObservableList<UserDetails> userDetailsList = FXCollections.observableArrayList();

    public Main() {
        // Add some sample data
        userDetailsList.add(new UserDetails("Hans"));
    }

    @Override
    public void start(Stage primaryStage) {

        this.primaryStage = primaryStage;
        primaryStage.show();
        primaryStage.setTitle("ICA");
        loadMainViewPage();
    }

    private void loadMainViewPage(){
        try{
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("MainView.fxml"));
            BorderPane mainView = (BorderPane) loader.load();
            Scene scene = new Scene(mainView);
            primaryStage.setScene(scene);
            MainViewController controller = loader.getController();
            controller.setUserDetailsList(userDetailsList);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public ObservableList<UserDetails> getUserDetailsList() {
        return userDetailsList;
    }
    public static void main(String[] args) {
        launch(args);
    }
}

Controller Class如下所示:

public class MainViewController {
    @FXML
    private TableView<UserDetails> userTable;
    @FXML
    private TableColumn<UserDetails,String> userNameColumn;
    private Main main;

     @FXML
     private void initialize() {
         System.out.println("inside initialize");
         userNameColumn.setCellValueFactory(cellData -> cellData.getValue().HostNameProperty());
     }

    public void setUserDetailsList(ObservableList<UserDetails> userDetailsList) {
        userTable.setItems(userDetailsList);

    }
}

FXML文件

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"     minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainViewController">
    <top>
        <MenuBar BorderPane.alignment="CENTER">
            <menus>
                <Menu mnemonicParsing="false" text="File">
                    <items>
                        <MenuItem mnemonicParsing="false" text="Close" />
                    </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                    <items>
                        <MenuItem mnemonicParsing="false" text="Delete" />
                    </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                    <items>
                        <MenuItem mnemonicParsing="false" text="About" />
                    </items>
                </Menu>
            </menus>
        </MenuBar>
    </top>
    <center>
        <AnchorPane>
            <children>
                <SplitPane dividerPositions="0.362876254180602" prefHeight="375.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <items>
                        <TableView fx:id="userTable">
                            <columns>
                                <TableColumn fx:id="userNameColumn" prefWidth="75.0" text="C1" />
                            </columns>
                            <columnResizePolicy>
                                <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                            </columnResizePolicy>
                        </TableView>
                        <TextArea />
                    </items>
                </SplitPane>
            </children>
        </AnchorPane>
    </center>
</BorderPane>

错误:

java.lang.NullPointerException
at application.Main.loadMainViewPage(Main.java:48)
at application.Main.start(Main.java:30)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)

0 个答案:

没有答案