2015-05-10 80 views
1

我有這個控制器類顯示一個TableView中數據庫查詢,但我有NullPointerException異常的錯誤與「setCellValueFactory(新PropertyValueFactory」NullPointerException異常JavaFX中初始化TableView中

package aplicativo; 

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.Button; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.TextField; 
import javafx.scene.control.cell.PropertyValueFactory; 

public class Controle implements Initializable{ 
    @FXML 
    private TextField txtCampo,txtCampo2; 
    @FXML 
    private Button btAdicionar,btConsultar; 
    @FXML 
    private TableView<Pessoa> tabValues; 
    @FXML 
    private TableColumn<Pessoa, Integer> tbcCod; 
    private TableColumn<Pessoa, String>tbcNome; 
    ObservableList<Pessoa> List = FXCollections.observableArrayList(); 
    @FXML 
    private void btAdd(){ 
     insertBD a = new insertBD(txtCampo.getText()); 
     consultaBD b = new consultaBD(); 
     List = b.consultaTudo(); 
     tabValues.setItems(List); 
     txtCampo.clear(); 
    } 
    @FXML 

    private void btCons(){ 
     String tx = txtCampo2.getText(); 
     if(tx.isEmpty()){ 

     }else{ 
     consultaBD a = new consultaBD(); 
     a.consultaParecido(tx, "nome"); 
     txtCampo2.clear(); 
     } 
    } 
    @Override 
    public void initialize(URL arg0, ResourceBundle arg1) { 

     // TODO Auto-generated method stub 
     tbcCod.setCellValueFactory(new PropertyValueFactory<Pessoa, Integer>("cod")); 
     tbcNome.setCellValueFactory(new PropertyValueFactory<Pessoa, String>("nome")); 
     tabValues.setItems(List); 
     tabValues.getColumns().addAll(tbcCod,tbcNome); 
    } 
} 

的NullPointerExcepetion:

Exception in Application start method 
java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source) 
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) 
Caused by: java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source) 
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/1645995473.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: javafx.fxml.LoadException: 
/C:/Users/lucas/workspace/BDFX/bin/aplicativo/Tela.fxml 

    at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.load(Unknown Source) 
    at aplicativo.Main.start(Main.java:13) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source) 
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/1031257736.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/186276003.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1529876784.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source) 
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source) 
    ... 1 more 
Caused by: java.lang.NullPointerException 
    at aplicativo.Controle.initialize(Controle.java:52) 
    ... 23 more 
Exception running application aplicativo.Main 

任何解決方案?

回答

4

一個在你的控制器實例字段是缺少@FXML註解。仙如果該字段是私有的,則FXML加載程序無法在加載期間將控制引用注入該字段。以下是您的實例字段聲明:

@FXML 
private TextField txtCampo,txtCampo2; 

@FXML 
private Button btAdicionar,btConsultar; 

@FXML 
private TableView<Pessoa> tabValues; 

@FXML 
private TableColumn<Pessoa, Integer> tbcCod; 

private TableColumn<Pessoa, String>tbcNome; 

請注意,最後一個字段tbcNome沒有註釋。因此,調用初始化方法時,tbcNome字段包含null引用,導致異常。

要解決您的問題,您可能需要做的就是將@FXML註釋添加到tbcNome的實例字段聲明中。

你可能鼓勵這個錯誤,採用在你的類型聲明中列出多個變量的習慣,例如。 private Button btAdicionar, btConsultar;。在我看來,這是一個壞習慣,可以鼓勵這樣的錯誤發生。我建議你嘗試採用每種實例字段都有自己的類型聲明語句的編碼風格。