gpt4 book ai didi

java - TableView JavaFX 应用程序线程“java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-02 03:21:27 25 4
gpt4 key购买 nike

我的程序正在尝试模拟一个存储产品信息的数据库。

我正在尝试学习如何使用 TableView,但它们仍然是我不明白的东西。在我的表中,我希望能够选择多个项目并对这些项目执行某些操作。我已经做到了。

但是如果我选择第 1 行和第 2 行并右键单击“ProductName”,则会出现一个问题。然后选择第 3 行并右键单击,单击 ProductName。它给了我一个 JavaFX 应用程序线程“java.lang.NullPointerException。

我不明白它为什么这样做。如果你能帮忙请帮忙。谢谢。对不起我的英语。

package TestTableView;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import static javafx.application.Application.launch;


public class TestTableView extends Application {
private Stage Listwindow;
private TableView<Product> table;
private String name;
private ObservableList<Product> itemSelected;
private WebView browser = new WebView();
private WebEngine webEngine = browser.getEngine();

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage){
//Load UnitTable
Listwindow = primaryStage;
Listwindow.setTitle("Product Table");

createTable();
createContextMenu();

VBox vBox = new VBox();
vBox.getChildren().addAll(table);
Scene scene = new Scene(vBox);
Listwindow.setScene(scene);
Listwindow.show();
}

public void createContextMenu(){
//Multiple selection
table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);


itemSelected = table.getSelectionModel().getSelectedItems();

//ContextMenu
ContextMenu contextMenu = new ContextMenu();

//menu items
MenuItem item1 = new MenuItem("ProductName");

//Add item to context menu
contextMenu.getItems().addAll(item1);

//set context menu to table
table.setContextMenu(contextMenu);


item1.setOnAction(e -> getProductName());
}

public void getProductName(){
for(int x = 0; x < itemSelected.size(); x++){
name = itemSelected.get(x).getName();

//do Something -> Name
}
table.getSelectionModel().clearSelection();
}

public void createTable(){

TableColumn<Product, String> nameCol = new TableColumn<>("Name");
nameCol.setMinWidth(200);
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));

TableColumn<Product, Double> priceCol = new TableColumn<>("Price");
priceCol.setMinWidth(200);
priceCol.setCellValueFactory(new PropertyValueFactory<>("price"));

TableColumn<Product, Integer> quantityCol = new TableColumn<>("Quantity");
quantityCol.setMinWidth(200);
quantityCol.setCellValueFactory(new PropertyValueFactory<>("quantity"));

table = new TableView<>();
table.setItems(getUnits());
table.getColumns().addAll(nameCol,priceCol,quantityCol);
}

public ObservableList<Product> getUnits(){

ObservableList<Product> unit = FXCollections.observableArrayList();
unit.add(new Product("Toilet",90.00,100));
unit.add(new Product("Chair",200.00,200));
unit.add(new Product("Avengers DVD",15.00,50));
return unit;
}
}

产品类别

    package TestTableView;

public class Product {
private String name;
private int quantity;
private double price;

Product(){
this.name = "";
this.price = 0;
this.quantity = 0;
}
Product(String Name, double price, int quantity) {
this.name = name;
this.quantity = quantity;
this.price = price;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public double getPrice() {
return price;
}

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

}

//

  Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at TestTableView.TestTableView.getProductName(TestTableView.java:73)
at TestTableView.TestTableView.lambda$createContextMenu$0(TestTableView.java:68)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

最佳答案

正如 BadVegan 建议在循环修复问题之前添加 itemSelected.sorted()

public void getProductName(){
itemSelected.sorted();
for(int x = 0; x < itemSelected.size(); x++){
name = itemSelected.get(x).getName();

//do Something -> Name
}
table.getSelectionModel().clearSelection();
}

关于java - TableView JavaFX 应用程序线程“java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39557979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com