我有这个控制器类用于显示方法的数据" getProduct"在TableView中,但我有一个与" tableViewItem.setItems(itemData)"相关的NullPointerException。我读了一些关于这个主题的帖子,但没有解决问题。
package configuradordepc.controller;
//All imports
public class ControladorView implements Initializable {
@FXML private MenuItem mNew, mOpen, mSave, mPrint;
@FXML
private MenuItem mMother, mCPU, mRAM, mGPU, mHardDrive, mCase, mKey, mMouse,
mScreen, mSpeak, mMulti, mDVD, mFan, mPower;
@FXML private Button bSearch, bAdd, bRemove, bClean;
@FXML private TableView tableViewItem;
@FXML private TableColumn<Product, String> nameItemColumn;
@FXML private TableColumn<Product, Double> priceColumn;
@FXML private TableColumn<Product, Integer> availableColumn;
private ObservableList<Product> itemData;
@FXML private TableView tableViewBudget;
@FXML private TableColumn<Product, String> nameBudgetColumn;
@FXML private TableColumn<Product, Double> outTaxColumn;
@FXML private TableColumn<Product, Double> inTaxColumn;
@FXML private TableColumn<Product, Integer> quantityColumn;
private ObservableList<Product> budgetData;
@FXML private TextField tFSearch, tFMin, tFMax;
//Errors from TableView
@Override
public void initialize(URL location, ResourceBundle resources) {
//Items Table
nameItemColumn.setCellValueFactory(
new PropertyValueFactory<>("description"));
priceColumn.setCellValueFactory(
new PropertyValueFactory<>("price"));
availableColumn.setCellValueFactory(
new PropertyValueFactory<>("stock"));
itemData = FXCollections.observableArrayList();
getProduct();
tableViewItem.setItems(itemData);
//Budget Table
nameBudgetColumn.setCellValueFactory(
new PropertyValueFactory<>("description"));
inTaxColumn.setCellValueFactory(
new PropertyValueFactory<>("price"));
budgetData = FXCollections.observableArrayList();
tableViewBudget.setItems(budgetData);
//Bindings Item
final BooleanBinding noItemSelectedI = Bindings.isNull(
tableViewItem.getSelectionModel().selectedItemProperty());
final BooleanBinding emptySearch = Bindings.isEmpty(tFSearch.textProperty());
//Bindings Budget
final BooleanBinding noItemSelectedB = Bindings.isNull(
tableViewBudget.getSelectionModel().selectedItemProperty());
final BooleanBinding emptyBudget = Bindings.isEmpty(tableViewBudget.getItems());
//Disable botones
bSearch.disableProperty().bind(emptySearch);
bAdd.disableProperty().bind(noItemSelectedI);
bRemove.disableProperty().bind(noItemSelectedB);
bClean.disableProperty().bind(emptyBudget);
}
@FXML
private void onSearch(ActionEvent event) {
}
@FXML
private void onAdd(ActionEvent event) {
budgetData.add((Product) tableViewItem.getSelectionModel().selectedItemProperty().getValue());
}
@FXML
private void onDelete(ActionEvent event) {
}
@FXML
private void onClean(ActionEvent event) {
}
//Item Table initialization
public void getProduct() {
//SPEAKER
itemData.add(new Product("Logitech Z213 Multimedia Speakers 2.1", 22.95, 50, SPEAKER));
//HDD
itemData.add(new Product("Seagate Barracuda 7200.14 1TB SATA3", 46.95, 100, HDD));
//HDD_SSD
itemData.add(new Product("Samsung 850 Evo SSD Series 250GB SATA3", 82, 65, HDD_SSD));
//POWER_SUPPLY
itemData.add(new Product("Tacens Mars Gaming 700W", 42.75, 25, POWER_SUPPLY));
//DVD_WRITER
itemData.add(new Product("LG GH24NSD1 Grabadora DVD 24x Negra", 12.95, 55, DVD_WRITER));
//RAM
itemData.add(new Product("G.Skill Ripjaws X DDR3 1600 PC3-12800 8GB 2x4GB CL9", 37.95, 80, RAM));
//SCREEN
itemData.add(new Product("LG 22M47VQ-P 21.5 LED", 115, 10, SCREEN));
//MULTIREADER
itemData.add(new Product("Unotec Lector USB de tarjetas SD/MicroSD", 3.95, 110, MULTIREADER));
//MOTHERBOARD
itemData.add(new Product("Gigabyte GA-H81M-S2H", 49.95, 75, MOTHERBOARD));
//CPU
itemData.add(new Product("Intel Core i5-4460 3.2Ghz Box", 175, 490, CPU));
//MOUSE
itemData.add(new Product("Logitech Wireless Mouse M175 Negro", 12.95, 200, MOUSE));
//GPU
itemData.add(new Product("Gigabyte GeForce GTX 970 Gaming G1 WindForce OC 4GB GDDR5", 354, 480, GPU));
//KEYBOARD
itemData.add(new Product("Nox Krom Kombat Teclado + Ratón", 30.99, 54, KEYBOARD));
//CASE
itemData.add(new Product("NOX Sense 500W + Frontal", 41.95, 65, CASE));
//FAN
itemData.add(new Product("Cooler Master Hyper TX3 EVO CPU Cooler", 21.50, 220, FAN));
}
}
这是输出:
ant -f C:\\Users\\Marco\\Desktop\\ConfiguradorDePC jfxsa-run
init:
Deleting: C:\Users\Marco\Desktop\ConfiguradorDePC\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\Marco\Desktop\ConfiguradorDePC\build\built-jar.properties
Compiling 2 source files to C:\Users\Marco\Desktop\ConfiguradorDePC\build\classes
Note: C:\Users\Marco\Desktop\ConfiguradorDePC\src\configuradordepc\controller\ControladorView.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
compile:
Deleting directory C:\Users\Marco\Desktop\ConfiguradorDePC\dist\lib
Copying 1 file to C:\Users\Marco\Desktop\ConfiguradorDePC\dist\lib
Detected JavaFX Ant API version 1.3
Launching <fx:jar> task from C:\Program Files\Java\jdk1.8.0_71\jre\..\lib\ant-javafx.jar
Warning: From JDK7u25 the Codebase manifest attribute should be used to restrict JAR repurposing.
Please set manifest.custom.codebase property to override the current default non-secure value '*'.
Launching <fx:deploy> task from C:\Program Files\Java\jdk1.8.0_71\jre\..\lib\ant-javafx.jar
No base JDK. Package will use system JRE.
No base JDK. Package will use system JRE.
jfx-deployment-script:
jfx-deployment:
jar:
Copying 13 files to C:\Users\Marco\Desktop\ConfiguradorDePC\dist\run150788111
jfx-project-run:
Executing C:\Users\Marco\Desktop\ConfiguradorDePC\dist\run150788111\ConfiguradorDePC.jar using platform C:\Program Files\Java\jdk1.8.0_71\jre/bin/java
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$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
file:/C:/Users/Marco/Desktop/ConfiguradorDePC/dist/run150788111/ConfiguradorDePC.jar!/configuradordepc/view/View.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at configuradordepc.ConfiguradorDePC.start(ConfiguradorDePC.java:15)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
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$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.NullPointerException
at configuradordepc.controller.ControladorView.initialize(ControladorView.java:75)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
Exception running application configuradordepc.ConfiguradorDePC
Java Result: 1
Deleting directory C:\Users\Marco\Desktop\ConfiguradorDePC\dist\run150788111
jfxsa-run:
BUILD SUCCESSFUL (total time: 3 seconds)
FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="configuradordepc.controller.ControladorView">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="Archivo">
<items>
<MenuItem fx:id="mNew" mnemonicParsing="false" text="Nuevo" />
<MenuItem fx:id="mOpen" mnemonicParsing="false" text="Abrir" />
<MenuItem fx:id="mSave" mnemonicParsing="false" text="Guardar" />
<MenuItem fx:id="mPrint" mnemonicParsing="false" text="Imprimir" />
</items>
</Menu>
</menus>
</MenuBar>
<Accordion VBox.vgrow="ALWAYS">
<panes>
<TitledPane animated="false" text="Nuevo PC">
<content>
<VBox fx:id="vBox" spacing="10.0">
<children>
<HBox spacing="10.0">
<children>
<TextField fx:id="tFSearch" HBox.hgrow="ALWAYS" />
<Button fx:id="bSearch" mnemonicParsing="false" onAction="#onSearch" text="Buscar" />
</children>
</HBox>
<HBox spacing="10.0" VBox.vgrow="ALWAYS">
<children>
<VBox spacing="10.0">
<children>
<MenuButton maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Categoría">
<items>
<MenuItem fx:id="mMother" mnemonicParsing="false" text="Placas base" />
<MenuItem fx:id="mCPU" mnemonicParsing="false" text="Procesadores" />
<MenuItem fx:id="mRAM" mnemonicParsing="false" text="Memorias RAM" />
<MenuItem fx:id="mGPU" mnemonicParsing="false" text="Tarjetas gráficas" />
<MenuItem fx:id="mHardDrive" mnemonicParsing="false" text="Discos duros" />
<MenuItem fx:id="mCase" mnemonicParsing="false" text="Torres" />
<MenuItem fx:id="mKey" mnemonicParsing="false" text="Teclados" />
<MenuItem fx:id="mMouse" mnemonicParsing="false" text="Ratones" />
<MenuItem fx:id="mScreen" mnemonicParsing="false" text="Monitores" />
<MenuItem fx:id="mSpeak" mnemonicParsing="false" text="Altavoces" />
<MenuItem fx:id="mMulti" mnemonicParsing="false" text="Multilectores" />
<MenuItem fx:id="mDVD" mnemonicParsing="false" text="Grabadoras" />
<MenuItem fx:id="mFan" mnemonicParsing="false" text="Ventiladores" />
<MenuItem fx:id="mPower" mnemonicParsing="false" text="Fuente de alimentación" />
</items>
</MenuButton>
<HBox alignment="CENTER_LEFT">
<children>
<Label prefWidth="87.0" text="Disponibilidad" />
<TextField fx:id="tFAvailable" HBox.hgrow="ALWAYS" />
</children>
</HBox>
<HBox alignment="CENTER_LEFT">
<children>
<Label prefWidth="87.0" text="Precio Mín." />
<TextField fx:id="tFMin" HBox.hgrow="ALWAYS" />
</children>
</HBox>
<HBox alignment="CENTER_LEFT">
<children>
<Label prefWidth="87.0" text="Precio Máx." />
<TextField fx:id="tFMax" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</children>
</VBox>
<VBox alignment="CENTER" spacing="10.0" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets />
</HBox.margin>
<children>
<TableView VBox.vgrow="ALWAYS">
<columns>
<TableColumn fx:id="nameItemColumn" editable="false" prefWidth="75.0" sortable="false" text="Nombre" />
<TableColumn fx:id="priceColumn" editable="false" prefWidth="75.0" sortable="false" text="Precio" />
<TableColumn fx:id="availableColumn" editable="false" prefWidth="75.0" sortable="false" text="Disponibilidad" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
<Button fx:id="bAdd" mnemonicParsing="false" onAction="#onAdd" text="Añadir" />
</children>
</VBox>
<VBox spacing="10.0" HBox.hgrow="ALWAYS">
<children>
<TableView VBox.vgrow="ALWAYS">
<columns>
<TableColumn fx:id="nameBudgetColumn" editable="false" prefWidth="75.0" sortable="false" text="Nombre" />
<TableColumn fx:id="outTaxColumn" editable="false" prefWidth="75.0" sortable="false" text="Sin IVA" />
<TableColumn fx:id="inTaxColumn" editable="false" prefWidth="75.0" sortable="false" text="Con IVA" />
<TableColumn fx:id="quantityColumn" prefWidth="75.0" sortable="false" text="Cantidad" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
<HBox alignment="CENTER" spacing="10.0">
<children>
<Button fx:id="bDelete" mnemonicParsing="false" onAction="#onDelete" text="Eliminar" />
<Button fx:id="bClean" mnemonicParsing="false" onAction="#onClean" text="Limpiar" />
</children>
</HBox>
</children>
</VBox>
</children>
</HBox>
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</VBox>
</content>
</TitledPane>
<TitledPane animated="false" text="Configuraciones" />
</panes>
</Accordion>
</children>
</VBox>
答案 0 :(得分:1)
您缺少表格中的fx:id
属性。你需要
<TableView fx:id="tableViewItem" VBox.vgrow="ALWAYS">
(或者只是将fx:id
添加到Scene Builder的“代码”部分的表中,如果您使用的是Scene Builder)。