空指针异常使用滚动窗格 javafX 时

我一直在构建一个电影院预订应用程序,并试图创建一个显示电影和放映时间的场景。当我使用锚点窗格和vbox显示所有信息时,它可以工作,但是当我尝试插入其他滚动窗格(在场景构建器中)时,FXML加载器返回一个空指针异常,我无法弄清楚为什么...


这是我的FXML代码


<?xml version="1.0" encoding="UTF-8"?>


<?import javafx.scene.control.*?>

<?import java.lang.*?>

<?import javafx.scene.layout.*?>


<AnchorPane prefHeight="598.0" prefWidth="798.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MovieShowingsController">

   <children>

      <MenuBar>

        <menus>

          <Menu mnemonicParsing="false" text="myBookings">

            <items>

              <MenuItem mnemonicParsing="false" text="Close" />

            </items>

          </Menu>

        </menus>

      </MenuBar>

      <ScrollPane fx:id="scrollpane" hbarPolicy="NEVER" layoutY="22.0" prefHeight="576.0" prefWidth="798.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="22.0">

         <content>

            <VBox fx:id="vbox" prefHeight="555.0" prefWidth="740.0" />

         </content>

      </ScrollPane>

   </children>

</AnchorPane>

主类


public class MovieShowings{


    private AnchorPane root;


    public MovieShowings() {


        try {


            root = FXMLLoader.load(getClass().getResource("movieshowings.fxml"));

        }


        catch(IOException e){


            e.printStackTrace();

        }


    }



    public Scene getScene() {


    Scene scene = new Scene(root,800,600);


    return scene;


    }


    public AnchorPane getRoot() {


        return root;

    }


}

和在用户登录后调用它的代码


if(DatabaseConnection.getInstance().login(Username.getText(), Password.getText())) {


            MovieShowings films = new MovieShowings();


            MovieShowingsController filmsController = new MovieShowingsController(films);


            Scene movieShowings = films.getScene();


            Stage window = (Stage) ((Node) e.getSource()).getScene().getWindow();


            window.setScene(movieShowings);


            window.show();

关于如何解决这个问题的任何想法?


编辑:fx:id 'vbox' 没有从 getRoot() 方法访问,即使已注入 FXML 加载程序


aluckdog
浏览 102回答 1
1回答

鸿蒙传说

这样做的原因是增加了内容,等等。在创建外观时,在第一个布局传递期间添加到场景。此布局传递发生在 JavaFX 应用程序线程“重新获得控制权”之后(即,您已完成事件处理程序、方法或让 JavaFX 执行代码的类似方式)。ScrollPaneScrollBarApplication.start请注意,您正在以一种非常奇怪的方式使用控制器类。我建议使用这个问题的答案中描述的方法之一与控制器进行通信:传递参数JavaFX FXML例如:public class MovieShowings{&nbsp; &nbsp; private AnchorPane root;&nbsp; &nbsp; public MovieShowings() {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FXMLLoader loader = new FXMLLoader(getClass().getResource("movieshowings.fxml"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root = loader.load();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MovieShowingsController controller = loader.getController();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controller.initMovieShowings(this);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(IOException e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; ...}public class MovieShowingsController {&nbsp; &nbsp; ...&nbsp; &nbsp; public void initMovieShowings(MovieShowings showings) {&nbsp; &nbsp; &nbsp; &nbsp; String date = "2019-04-15";&nbsp; &nbsp; &nbsp; &nbsp; Date sqlDate = Date.valueOf(date);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("***Screenings for " + date + "***");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;filmList = new ArrayList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;screeningList = DatabaseConnection.getInstance().retrieveScreeningsForDay(sqlDate);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (Screening screeningInstance : screeningList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!filmList.contains(screeningInstance.getFilmInfo())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;filmList.add(screeningInstance.getFilmInfo());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(screeningInstance.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(screeningList);&nbsp; &nbsp; &nbsp; &nbsp; this.showings = showings;&nbsp; &nbsp; &nbsp; &nbsp; //populating FXML instance variable from loader&nbsp; &nbsp; &nbsp; &nbsp; // use the injected field here&nbsp; &nbsp; &nbsp; &nbsp; buildMovieShowings(vbox);&nbsp; &nbsp; }&nbsp; &nbsp; ...}由于您实际上并没有在控制器中使用该对象,因此可以通过从MovieShowings@FXMLprivate void initialize()方法,并从控制器代码中删除每个与之相关的部分。通过这种方式,您可以摆脱将其传递给控制器的必要性。MovieShowings使用使用自定义单元格也可以是显示电影的选项...ListView
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java