JavaFX TableView值为Null

时间:2016-08-16 21:50:07

标签: javafx tableview

我每次尝试运行此程序时都会收到一些我不理解的错误。只有当我设置了以下行BaseColorColumn.setCellValueFactory(new PropertyValueFactory<BaseColor, String>("BaseColor")); PriceColumn.setCellValueFactory(new PropertyValueFactory<BaseColor, Integer>("Price")); 时才会触发错误。我相信他们会返回NULL,但我不确定原因。我基本上只是试图用CustomerTableView

中的数据填写名为BaseColor的表格

// FXML

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

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

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="supremeinkcalcmk2.MainController">
   <left>
      <VBox prefHeight="400.0" prefWidth="152.0" BorderPane.alignment="CENTER">
         <children>
            <TableView prefHeight="404.0" prefWidth="152.0">
              <columns>
                <TableColumn editable="false" prefWidth="75.0" sortable="false" text="Formla" />
                <TableColumn editable="false" prefWidth="75.0" sortable="false" text="Price" />
              </columns>
            </TableView>
         </children>
      </VBox>
   </left>
   <right>
      <VBox prefHeight="400.0" prefWidth="152.0" BorderPane.alignment="CENTER">
         <children>
            <ComboBox fx:id="ComboBoxSelectCustomer" prefWidth="150.0" promptText="Select Customer" />
            <TableView fx:id="CustomerTableView" prefHeight="266.0" prefWidth="152.0">
              <columns>
                <TableColumn fx:id="BaseColor" prefWidth="75.0" text="Base Color" />
                <TableColumn fx:id="Price" editable="false" prefWidth="75.0" sortable="false" text="Price" />
              </columns>
            </TableView>
            <Button fx:id="ButtonSaveCustomer" mnemonicParsing="false" prefHeight="25.0" prefWidth="152.0" text="Save Customer" />
         </children>
      </VBox>
   </right>
   <center>
      <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <Label layoutX="103.0" layoutY="122.0" text="Pantone Number" />
            <TextField layoutX="74.0" layoutY="139.0" />
            <Label fx:id="PriceLabel" layoutX="132.0" layoutY="293.0" />
            <Button fx:id="ButtonCalculate" layoutX="113.0" layoutY="200.0" mnemonicParsing="false" onAction="#CalculateButton" text="Calculate" />
            <Label layoutX="131.0" layoutY="285.0" text="Label" />
         </children>
      </Pane>
   </center>
</BorderPane>

// MainController.Java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package supremeinkcalcmk2;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

/**
 * FXML Controller class
 *
 * @author Archa
 */

public class MainController implements Initializable {

    @FXML public ComboBox ComboBoxSelectCustomer;
    @FXML private TableView<BaseColor> CustomerTableView;
    @FXML private TableColumn<BaseColor, String> BaseColorColumn;
    @FXML private TableColumn<BaseColor, Integer> PriceColumn;

    //Customer TableView
    ObservableList<BaseColor> data = FXCollections.observableArrayList(
        new BaseColor("Yellow", 0),
        new BaseColor("Green", 0),
        new BaseColor("Blue", 0)
    );

    /**
     * Initializes the controller class.
     */    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO

        //CustomerTableView
        BaseColorColumn.setCellValueFactory(new PropertyValueFactory<BaseColor, String>("BaseColor"));
        PriceColumn.setCellValueFactory(new PropertyValueFactory<BaseColor, Integer>("Price"));
        CustomerTableView.setItems(data);
    }    
    public void CalculateButton(){
        System.out.print("it is working!");
    }
}

// BaseColor.Java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package supremeinkcalcmk2;

/**
 *
 * @author Arch
 */
public class BaseColor {
    private String BaseColor;
    private double Price;

    public BaseColor(String BaseColor, double Price){
        this.BaseColor = "";
        this.Price = 0;
    }

    public String getBaseColor() {
        return BaseColor;
    }

    public void setBaseColor(String BaseColor) {
        this.BaseColor = BaseColor;
    }

    public double getPrice() {
        return Price;
    }

    public void setPrice(double Price) {
        this.Price = Price;
    }

}

//错误日志

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/1343441044.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
file:/D:/Programming/SupremeInkCalcMk2/dist/run103801275/SupremeInkCalcMk2.jar!/supremeinkcalcmk2/Main.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2583)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
    at supremeinkcalcmk2.SupremeInkCalcMk2.start(SupremeInkCalcMk2.java:33)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/726585699.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/355629945.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1149823713.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1915503092.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
    ... 1 more
Caused by: java.lang.NullPointerException
    at supremeinkcalcmk2.MainController.initialize(MainController.java:52)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    ... 22 more
Exception running application supremeinkcalcmk2.SupremeInkCalcMk2
Java Result: 1

1 个答案:

答案 0 :(得分:2)

您的fx:id与字段名称不匹配:

<TableColumn fx:id="BaseColor" prefWidth="75.0" text="Base Color" />
<TableColumn fx:id="Price" editable="false" prefWidth="75.0" sortable="false" text="Price" />

@FXML private TableColumn<BaseColor, String> BaseColorColumn;
@FXML private TableColumn<BaseColor, Integer> PriceColumn;