2016-09-18 84 views
0

我的程序試圖模擬存儲產品信息的數據庫。的TableView JavaFX應用程序線程」顯示java.lang.NullPointerException

我想學習如何使用的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) 
+3

[編輯]你的問題包括:完成[堆棧跟蹤](http://stackoverflow.com/q/3988788/2775450)並確定代碼中的哪一行正在拋出異常。 –

+0

*「它給了我一個JavaFX應用程序線程java.lang.NullPointerException。*」 - 此信息對於解決您的問題至關重要;請將其添加到帖子中。 –

+0

對不起,我已經包含堆棧跟蹤 –

回答

2

作爲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(); 
}  
0

這必須是正確執行Product class.Don't 使用干將導致當你改變值的表沒有得到 刷新:

import javafx.beans.property.DoubleProperty; 
import javafx.beans.property.IntegerProperty; 
import javafx.beans.property.SimpleDoubleProperty; 
import javafx.beans.property.SimpleIntegerProperty; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 

public class Product { 

    private StringProperty name; 
    private DoubleProperty price; 
    private IntegerProperty quantity; 

    /** 
    * Constructor 
    */ 
    protected Product() { 
     name = new SimpleStringProperty(""); 
     price = new SimpleDoubleProperty(0.0); 
     quantity = new SimpleIntegerProperty(0); 

    } 

    /** 
    * Constructor 
    * 
    * @param name 
    * @param price 
    * @param quantity 
    */ 
    protected Product(String name, double price, int quantity) { 
     this.name = new SimpleStringProperty(name); 
     this.price = new SimpleDoubleProperty(price); 
     this.quantity = new SimpleIntegerProperty(quantity); 
    } 

    public StringProperty nameProperty() { 
     return name; 
    } 

    public DoubleProperty priceProperty() { 
     return price; 
    } 

    public IntegerProperty quantityProperty() { 
     return quantity; 
    } 

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

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

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

} 
+1

謝謝,我也會試試這個。 –

相關問題