JavaFX,用bean和bigDecimal为TableView初始化setCellValueFactory,结果 - >空指针异常

时间:2016-09-19 08:21:53

标签: javafx tableview javabeans bigdecimal tablecolumn

我是JavaFX的新手,我想在eval的{​​{1}}中打印一些BigDecimalBean。 问题是,当我尝试TableColumn的{​​{1}}时,我会收到TableView

为了清楚我正在使用带有场景构建器的NullPointerExceptionexception图形(面板,tableView等)和用于将bean的数据链接到tableView的java类。我正在使用setItems来获取更改,而Bean具有返回TableViewfxml的正确getter。

我不知道我用于ObservableList的内容是否正确,我已尝试按照教程here进行操作,但在本教程中,ObjectProperty<BigDecimal>仅设置为ObjectProperty<ContoBean>。我已经阅读了setCellValueFactory的一些示例,但据我所知,最好的方法是避免使用TableColumn并尝试坚持使用lambda表达式(更安全并保留数据类型)完整使用String,如果我错了,请纠正我。

我的问题是: 使用setCellValueFactory PropertyValueFactorycellFactory和Bean的最佳实践是什么? 最好的方法是使用场景构建器创建TableColumns并将它们与控制器链接,或者在场景构建器中仅创建TableView,并在Java控制器中创建每个TableColumn? 如果我使用包含其他Bean的Bean,TableColumn将如何表示信息?哪个变量将用于查看信息,所有Bean的变量或仅一个变量?

MovimentoOverview.fxml

setCellValueFactory

MovimentoOverviewController

TableColumn

MainApp

BigDecimal

MovimentoBean

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

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="900.0" prefWidth="1400.0" stylesheets="@DarkTheme.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="it.evil.money.view.MovimentoOverviewController">
   <children>
      <SplitPane dividerPositions="0.12857142857142856" prefHeight="300.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <TreeView layoutX="-9.0" layoutY="14.0" prefHeight="891.0" prefWidth="200.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="5.0" />
               </children>
            </AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" styleClass="background">
               <children>
                  <Label layoutX="14.0" layoutY="7.0" styleClass="label-header" text="Person Details" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="5.0">
                     <font>
                        <Font size="18.0" />
                     </font></Label>
                  <ToolBar layoutX="216.0" layoutY="258.0" prefHeight="40.0" styleClass="background" AnchorPane.bottomAnchor="10.0" AnchorPane.rightAnchor="10.0">
                    <items>
                      <Button mnemonicParsing="false" onAction="#handleNewMovimento" text="New.." />
                        <Button mnemonicParsing="false" onAction="#handleEditMovimento" text="Edit.." />
                        <Button alignment="CENTER_RIGHT" mnemonicParsing="false" onAction="#handleDeleteMovimento" text="Delete.." textAlignment="CENTER" />
                    </items>
                  </ToolBar>
                  <TableView layoutX="5.0" layoutY="55.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="100.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="60.0">
                    <columns>
                      <TableColumn fx:id="idColumn" prefWidth="75.0" text="ID" />
                      <TableColumn fx:id="contoColumn" prefWidth="75.0" text="Conto" />
                        <TableColumn fx:id="dataColumn" prefWidth="75.0" text="Data" />
                        <TableColumn fx:id="valutaColumn" prefWidth="75.0" text="Valuta" />
                        <TableColumn fx:id="importoColumn" prefWidth="75.0" text="Importo" />
                        <TableColumn fx:id="quotaTicketColumn" prefWidth="75.0" text="Quota Ticket" />
                        <TableColumn fx:id="causaleColumn" prefWidth="75.0" text="Causale" />
                        <TableColumn fx:id="noteColumn" prefWidth="75.0" text="Note" />
                        <TableColumn fx:id="tipoPagamentoColumn" prefWidth="75.0" text="Tipo Pagamento" />
                        <TableColumn fx:id="tipoMovimentoColumn" prefWidth="75.0" text="Tipo Movimento" />
                        <TableColumn fx:id="spesaInComuneColumn" prefWidth="75.0" text="Spesa in Comune" />
                        <TableColumn fx:id="spesaPerLavoroColumn" prefWidth="75.0" text="Spesa per Lavoro" />
                        <TableColumn fx:id="utenteColumn" prefWidth="75.0" text="Utente" />
                        <TableColumn fx:id="tagColumn" prefWidth="75.0" text="Tag" />
                        <TableColumn fx:id="bustaPagaColumn" prefWidth="75.0" text="Busta Paga" />
                    </columns>
                  </TableView>
               </children>
            </AnchorPane>
        </items>
      </SplitPane>
   </children>
</AnchorPane>

当我启动程序时,我收到此错误:

package it.evil.money.view;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import java.math.BigDecimal;
import java.time.LocalDate;

import it.evil.money.MainApp;
import it.evil.money.model.BustaPagaBean;
import it.evil.money.model.CausaleBean;
import it.evil.money.model.ContoBean;
import it.evil.money.model.MovimentoBean;
import it.evil.money.model.TagBean;
import it.evil.money.model.TipoMovimentoBean;
import it.evil.money.model.TipoPagamentoBean;
import it.evil.money.model.UtenteBean;
import it.evil.money.util.DateUtil;

public class MovimentoOverviewController {

    @FXML
    private TableView<MovimentoBean> movimentoTable;
    @FXML
    private TableColumn<MovimentoBean, BigDecimal> idColumn;
    @FXML
    private TableColumn<MovimentoBean, ContoBean> contoColumn;
    @FXML
    private TableColumn<MovimentoBean, LocalDate> dataColumn;
    @FXML
    private TableColumn<MovimentoBean, String> valutaColumn;
    @FXML
    private TableColumn<MovimentoBean, BigDecimal> importoColumn;
    @FXML
    private TableColumn<MovimentoBean, BigDecimal> quotaTicketColumn;
    @FXML
    private TableColumn<MovimentoBean, CausaleBean> causaleColumn;
    @FXML
    private TableColumn<MovimentoBean, String> noteColumn;
    @FXML
    private TableColumn<MovimentoBean, TipoPagamentoBean> tipoPagamentoColumn;
    @FXML
    private TableColumn<MovimentoBean, TipoMovimentoBean> tipoMovimentoColumn;
    @FXML
    private TableColumn<MovimentoBean, Boolean> spesaInComuneColumn;
    @FXML
    private TableColumn<MovimentoBean, Boolean> spesaPerLavoroColumn;
    @FXML
    private TableColumn<MovimentoBean, BustaPagaBean> bustaPagaColumn;
    @FXML
    private TableColumn<MovimentoBean, UtenteBean> utenteColumn;
    @FXML
    private TableColumn<MovimentoBean, TagBean> tagColumn;


    @FXML
    private Label idLabel;
    @FXML
    private Label contoLabel;
    @FXML
    private Label dataLabel;
    @FXML
    private Label valutaLabel;
    @FXML
    private Label importoLabel;

    // Reference to the main application.
    private MainApp mainApp;



    public MovimentoOverviewController() {
    }


    @FXML
    private void initialize() {
        idColumn.setCellValueFactory(cellData -> cellData.getValue().idProperty() );
        contoColumn.setCellValueFactory(cellData -> cellData.getValue().contoProperty() );
        dataColumn.setCellValueFactory(cellData -> cellData.getValue().dataProperty() );
        valutaColumn.setCellValueFactory(cellData -> cellData.getValue().valutaProperty() );
        importoColumn.setCellValueFactory(cellData -> cellData.getValue().importoProperty() );

        quotaTicketColumn.setCellValueFactory(cellData -> cellData.getValue().quotaTicketProperty() );
        causaleColumn.setCellValueFactory(cellData -> cellData.getValue().causaleProperty() );
        noteColumn.setCellValueFactory(cellData -> cellData.getValue().noteProperty() );
        tipoPagamentoColumn.setCellValueFactory(cellData -> cellData.getValue().tipoPagamentoProperty() );
        tipoMovimentoColumn.setCellValueFactory(cellData -> cellData.getValue().tipoMovimentoProperty() );
        spesaInComuneColumn.setCellValueFactory(cellData -> cellData.getValue().spesaInComuneProperty() );
        spesaPerLavoroColumn.setCellValueFactory(cellData -> cellData.getValue().spesaPerLavoroProperty() );
        bustaPagaColumn.setCellValueFactory(cellData -> cellData.getValue().bustaPagaProperty() );
        utenteColumn.setCellValueFactory(cellData -> cellData.getValue().utenteProperty() );
        tagColumn.setCellValueFactory(cellData -> cellData.getValue().tagProperty() );

    }

    public void setMainApp(MainApp mainApp) {
        this.mainApp = mainApp;
        movimentoTable.setItems(mainApp.getMovimentoData());
    }

}

0 个答案:

没有答案