JavaFX中的NullPointerException在TableView中初始化 [英] NullPointerException in JavaFX initialize in TableView

查看:123
本文介绍了JavaFX中的NullPointerException在TableView中初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个控制器类用于在TableView中显示数据库查询,但是我的错误是NullPointerException,带有setCellValueFactory(new PropertyValueFactory

I have this controller class for showing a database query in a TableView, but i am having error of NullPointerException with the "setCellValueFactory(new PropertyValueFactory"

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:

The 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

任何解决方案?

推荐答案

控制器中的一个实例字段是缺少 @FXML 注释。由于该字段是私有的,因此FXML加载程序无法在加载期间将控制引用注入字段。以下是您的实例字段声明:

One of the instance fields in your controller is lacking an @FXML annotation. Since the field is private, the FXML loader is unable to inject the control reference into the field during loading. Here are your instance field declarations:

@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未注释。因此,当调用initialize方法时, tbcNome 字段包含 null 引用,从而导致异常。

Notice that the last field, tbcNome, is not annotated. As a result, when your initialize method is called, the tbcNome field contains a null reference, resulting in the exception.

要解决您的问题,您可能需要做的就是将 @FXML 注释添加到实例字段声明中对于 tbcNome

To fix your problem, all you may need to do is add the @FXML annotation to the instance field declaration for tbcNome.

您可能通过在您的类型中列出多个变量的习惯来鼓励此错误声明,例如。 private Button btAdicionar,btConsultar; 。在我看来,这是一个坏习惯,可以鼓励这样的错误发生。我建议您尝试采用编码样式,其中每个实例字段都有自己的类型声明语句。

You may have encouraged this error by adopting the habit of listing more than one variable in your type declarations, eg. private Button btAdicionar, btConsultar;. In my opinion, this is a bad habit that can encourage errors like this to happen. I would suggest that you try to adopt the coding style in which each instance field has its own type declaration statement.

这篇关于JavaFX中的NullPointerException在TableView中初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆