亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

空指針異常使用滾動窗格 javafX 時

空指針異常使用滾動窗格 javafX 時

aluckdog 2022-08-17 10:17:44
我一直在構建一個電影院預訂應用程序,并試圖創建一個顯示電影和放映時間的場景。當我使用錨點窗格和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 加載程序
查看完整描述

1 回答

?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

這樣做的原因是增加了內容,等等。在創建外觀時,在第一個布局傳遞期間添加到場景。此布局傳遞發生在 JavaFX 應用程序線程“重新獲得控制權”之后(即,您已完成事件處理程序、方法或讓 JavaFX 執行代碼的類似方式)。ScrollPaneScrollBarApplication.start


請注意,您正在以一種非常奇怪的方式使用控制器類。我建議使用這個問題的答案中描述的方法之一與控制器進行通信:傳遞參數JavaFX FXML


例如:


public class MovieShowings{


    private AnchorPane root;


    public MovieShowings() {


        try {

            FXMLLoader loader = new FXMLLoader(getClass().getResource("movieshowings.fxml"));

            root = loader.load();

            MovieShowingsController controller = loader.getController();

            controller.initMovieShowings(this);

        }


        catch(IOException e){


            e.printStackTrace();

        }


    }

    ...


}

public class MovieShowingsController {


    ...


    public void initMovieShowings(MovieShowings showings) {

        String date = "2019-04-15";

        Date sqlDate = Date.valueOf(date);


         System.out.println("\n");

         System.out.println("***Screenings for " + date + "***");


         filmList = new ArrayList();

         screeningList = DatabaseConnection.getInstance().retrieveScreeningsForDay(sqlDate);


         for (Screening screeningInstance : screeningList) {


             if (!filmList.contains(screeningInstance.getFilmInfo())) {


                 filmList.add(screeningInstance.getFilmInfo());


             }


             System.out.println(screeningInstance.toString());

          }


        Collections.sort(screeningList);


        this.showings = showings;


        //populating FXML instance variable from loader


        // use the injected field here

        buildMovieShowings(vbox);

    }


    ...

}

由于您實際上并沒有在控制器中使用該對象,因此可以通過從MovieShowings


@FXML

private void initialize()

方法,并從控制器代碼中刪除每個與之相關的部分。通過這種方式,您可以擺脫將其傳遞給控制器的必要性。MovieShowings


使用使用自定義單元格也可以是顯示電影的選項...ListView


查看完整回答
反對 回復 2022-08-17
  • 1 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號