(我的英语不好。)
我是javafx的新秀,这是我第一次发布到论坛。
我有一个有关剧院预订的项目。我的程序中有3个剧院,我正在使用标签在每个剧院中显示电影数据,但是当我叫NullPointerException
时得到setText()
。
我检查了fx:controller
和fx:id
没关系。我还尝试从标签打印getText()
,但得到同样的错误。 (我已经在标签上设置了文本“无电影”。)
这是我的代码
MainController:
package movie.view;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import movie.Main;
public class MainPageController {
private Main main;
@FXML public Label theaterData1;
@FXML public Label theaterData2;
@FXML public Label theaterData3;
private String display;
public MainPageController() {}
@FXML
private void goLogInPage() throws IOException{
main.showLogInPageScene();
}
public void recieveData(String data)
{
display = data;
}
@FXML
public void setDataForTheater1()
{
theaterData1.setText(display);
}
@FXML
public void setDataForTheater2()
{
theaterData2.setText(display);
}
@FXML
public void setDataForTheater3()
{
theaterData3.setText(display);
}
}
MainView FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane prefHeight="768.0" prefWidth="1024.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="movie.view.MainPageController">
<bottom>
<HBox alignment="BOTTOM_RIGHT" prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Button mnemonicParsing="false" onAction="#goLogInPage" text="For Admin Only">
<HBox.margin>
<Insets bottom="15.0" right="15.0" />
</HBox.margin>
</Button>
</children>
</HBox>
</bottom>
<top>
<Label text="Welcome to CSCU Theater" BorderPane.alignment="CENTER">
<font>
<Font size="30.0" />
</font>
<BorderPane.margin>
<Insets top="20.0" />
</BorderPane.margin>
</Label>
</top>
<center>
<GridPane BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="852.0" minWidth="10.0" prefWidth="817.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="502.0" minWidth="10.0" prefWidth="207.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="90.0" minHeight="10.0" prefHeight="51.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="159.0" minHeight="10.0" prefHeight="150.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Theater 1">
<font>
<Font size="24.0" />
</font>
<GridPane.margin>
<Insets left="20.0" />
</GridPane.margin>
</Label>
<Label fx:id="theaterData1" text="No movie avaliable :(" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="20.0" />
</GridPane.margin>
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</GridPane>
<GridPane GridPane.rowIndex="1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="90.0" minHeight="10.0" prefHeight="55.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="146.0" minHeight="10.0" prefHeight="146.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Theater 2">
<font>
<Font size="24.0" />
</font>
<GridPane.margin>
<Insets left="20.0" />
</GridPane.margin>
</Label>
<Label fx:id="theaterData2" text="No movie avaliable :(" GridPane.rowIndex="1">
<font>
<Font size="20.0" />
</font>
<GridPane.margin>
<Insets left="20.0" />
</GridPane.margin>
</Label>
</children>
</GridPane>
<GridPane GridPane.rowIndex="2">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="90.0" minHeight="10.0" prefHeight="54.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="147.0" minHeight="10.0" prefHeight="147.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Theater 3">
<padding>
<Insets left="20.0" />
</padding>
<font>
<Font size="24.0" />
</font>
</Label>
<Label fx:id="theaterData3" text="No movie avaliable :(" GridPane.rowIndex="1">
<font>
<Font size="20.0" />
</font>
<GridPane.margin>
<Insets left="20.0" />
</GridPane.margin>
</Label>
</children>
</GridPane>
<GridPane maxHeight="150.0" vgap="5.0" GridPane.columnIndex="1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Select Round">
<font>
<Font size="18.0" />
</font>
</Label>
<ComboBox prefWidth="150.0" GridPane.rowIndex="1" />
<Button mnemonicParsing="false" text="Select Seat" GridPane.rowIndex="2" />
</children>
</GridPane>
<GridPane maxHeight="150.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Select Round">
<font>
<Font size="18.0" />
</font>
</Label>
<ComboBox prefWidth="150.0" GridPane.rowIndex="1" />
<Button mnemonicParsing="false" text="Select Seat" GridPane.rowIndex="2" />
</children>
</GridPane>
<GridPane maxHeight="150.0" GridPane.columnIndex="1" GridPane.rowIndex="2">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Select Round">
<font>
<Font size="18.0" />
</font>
</Label>
<ComboBox prefWidth="150.0" GridPane.rowIndex="1" />
<Button mnemonicParsing="false" text="Select Seat" GridPane.rowIndex="2" />
</children>
</GridPane>
</children>
</GridPane>
</center>
</BorderPane>
我在这里得到NullPointerException
theaterData1.setText(display);
任何帮助将不胜感激,请解释为什么会发生此错误。