首页 \ 问答 \ java.lang.reflect.InvocationTargetException JavaFX TableView(java.lang.reflect.InvocationTargetException JavaFX TableView)

java.lang.reflect.InvocationTargetException JavaFX TableView(java.lang.reflect.InvocationTargetException JavaFX TableView)

我在尝试使用cellData - > cellData.getValue()从对象中添加属性时遇到了此问题,该属性是一个StringProperty,我有一个方法将其返回,并在cellData中使用它 - > cellData.getValue()。methodtoreturnproperty,但它仍然给我java.lang.reflect.InvocationTargetException,有没有人知道我做错了什么? 代码如下:

   package projeto.resources;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import projeto.Filmes;
import projeto.MainApp;
import projeto.Sala;

public class FilmeOverviewController {

    @FXML
    private TableView<Filmes> filmeTable;
    @FXML
    private TableColumn<Filmes, String> nomeColumn;
    @FXML
    private TableColumn<Filmes, String> categoriaColumn;
    @FXML
    private TableColumn<Filmes, String> salaColumn;

    @FXML
    private Label nomeLabel;
    @FXML
    private Label salaLabel;
    @FXML
    private Label categoriaLabel;
    @FXML
    private Label diretorLabel;
    @FXML
    private Label duracaoLabel;
    @FXML
    private Label protagonistaLabel;
    @FXML
    private Label classificacaoLabel;

    // Reference to the main application.
    private MainApp mainApp;

    public FilmeOverviewController() {
    }

    @FXML
    private void initialize() {
        //Inicia a tableview com tres colunas.
        nomeColumn.setCellValueFactory(cellData -> cellData.getValue().nomeProperty());
        categoriaColumn.setCellValueFactory(cellData -> cellData.getValue().categoriaProperty());
        salaColumn.setCellValueFactory(cellData -> cellData.getValue().numeroProperty());
        // limpando os detalhes
        showFilmeDetails(null);
        // adicionando funcao
        filmeTable.getSelectionModel().selectedItemProperty()
                .addListener((observable, oldValue, newValue) -> showFilmeDetails(newValue));

    }

    public void setMainApp(MainApp mainApp) {
        this.mainApp = mainApp;

        //adiciona uma observable list
        filmeTable.setItems(mainApp.getfilmeDados());
    }

    private void showFilmeDetails(Filmes filme) {
        if (filme != null) {
            nomeLabel.setText(filme.getNome());
            categoriaLabel.setText(filme.getCategoria());
            duracaoLabel.setText(filme.getDuracao());
            protagonistaLabel.setText(filme.getProtagonista());
            classificacaoLabel.setText(filme.getClassificacao());
            diretorLabel.setText(filme.getDiretor());
            salaLabel.setText(filme.getSalaN());
        } else {
            nomeLabel.setText("");
            categoriaLabel.setText("");
            duracaoLabel.setText("");
            protagonistaLabel.setText("");
            classificacaoLabel.setText("");
            diretorLabel.setText("");
            salaLabel.setText("");
        }
    }

    @FXML
    private void handleDeletarFilme() {
        int selectedIndex = filmeTable.getSelectionModel().getSelectedIndex();
        if (selectedIndex >= 0) {
            filmeTable.getItems().remove(selectedIndex);
        } else {
            Alert alerta = new Alert(AlertType.WARNING);
            alerta.setTitle("Nenhum filme selecionado");
            alerta.setHeaderText("Nenhuma Selecao");
            alerta.setContentText("Por favor selecione um filme para deletar");
            alerta.showAndWait();
        }

    }

    @FXML
    private void handleNovoFilme() {
        Filmes tempFilme = new Filmes("Nome","Categoria");
        boolean clicado = mainApp.showEditarFilmeDialog(tempFilme);
        if (clicado) {
            mainApp.getfilmeDados().add(tempFilme);
        }

    }

    @FXML
    private void handleEditarFilme() {
        Filmes filmeSelecionado = filmeTable.getSelectionModel().getSelectedItem();
            if(filmeSelecionado != null) {
                boolean clicado = mainApp.showEditarFilmeDialog(filmeSelecionado);
                if(clicado) {
                    showFilmeDetails(filmeSelecionado);
                }
            }else {
                //se nada for selecionado
                Alert alerta = new Alert(AlertType.WARNING);
                alerta.initOwner(mainApp.getPrimaryStage());
                alerta.setTitle("Nenhuma selecao");
                alerta.setHeaderText("Nenhum filme selecionado");
                alerta.setContentText("Por favor selecione algum filme.");
                alerta.showAndWait();
            }
    }
}

有一个名为Sala的类,它被导入到这个控制器中,我可以让它与另一个名为Filmes的类一起工作,我不知道它为什么不与Sala类一起工作,这里是类中的代码:

package projeto;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Sala {
    private boolean e3d;
    private int assentosMax;
    private int assentosDisp;
    private final StringProperty numeroProperty = new SimpleStringProperty();


    public Sala(boolean e3d, int assentosMax, int assentosDisp, String numero) {
        setNumero(numero);
        e3d = this.e3d;
        assentosMax = this.assentosMax;
        assentosDisp = this.assentosDisp;
    }
    public boolean isE3d() {
        return e3d;
    }
    public void setE3d(boolean e3d) {
        this.e3d = e3d;
    }
    public int getAssentosMax() {
        return assentosMax;
    }
    public void setAssentosMax(int assentosMax) {
        this.assentosMax = assentosMax;
    }
    public int getAssentosDisp() {
        return assentosDisp;
    }
    public void setAssentosDisp(int assentosDisp) {
        this.assentosDisp = assentosDisp;
    }
    public StringProperty numeroProperty() {
        return numeroProperty;
    }
    public final String getNumero() {
        return numeroProperty.get();
    }
    public final void setNumero(String numero) {
        numeroProperty().set(numero);
    }



}

编辑:这是错误:

jun 07, 2018 3:18:37 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 9.0.1 by JavaFX runtime of version 8.0.171
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:498)
    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:498)
    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$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.Error: Unresolved compilation problem: 
    The method numeroProperty() is undefined for the type Filmes

    at projeto.resources.FilmeOverviewController.<init>(FilmeOverviewController.java:50)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at projeto.MainApp.showFilmeOverview(MainApp.java:59)
    at projeto.MainApp.start(MainApp.java:50)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
    ... 1 more
Exception running application projeto.MainApp

提前致谢!


I've encountered this problem while trying to use cellData -> cellData.getValue() to add an attribute from an object, the attribute is a StringProperty and i have a method to return it, and i'm using it in the cellData -> cellData.getValue().methodtoreturnproperty, but it's still giving me java.lang.reflect.InvocationTargetException, does anyone know what i'm doing wrong? Here's the code:

   package projeto.resources;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import projeto.Filmes;
import projeto.MainApp;
import projeto.Sala;

public class FilmeOverviewController {

    @FXML
    private TableView<Filmes> filmeTable;
    @FXML
    private TableColumn<Filmes, String> nomeColumn;
    @FXML
    private TableColumn<Filmes, String> categoriaColumn;
    @FXML
    private TableColumn<Filmes, String> salaColumn;

    @FXML
    private Label nomeLabel;
    @FXML
    private Label salaLabel;
    @FXML
    private Label categoriaLabel;
    @FXML
    private Label diretorLabel;
    @FXML
    private Label duracaoLabel;
    @FXML
    private Label protagonistaLabel;
    @FXML
    private Label classificacaoLabel;

    // Reference to the main application.
    private MainApp mainApp;

    public FilmeOverviewController() {
    }

    @FXML
    private void initialize() {
        //Inicia a tableview com tres colunas.
        nomeColumn.setCellValueFactory(cellData -> cellData.getValue().nomeProperty());
        categoriaColumn.setCellValueFactory(cellData -> cellData.getValue().categoriaProperty());
        salaColumn.setCellValueFactory(cellData -> cellData.getValue().numeroProperty());
        // limpando os detalhes
        showFilmeDetails(null);
        // adicionando funcao
        filmeTable.getSelectionModel().selectedItemProperty()
                .addListener((observable, oldValue, newValue) -> showFilmeDetails(newValue));

    }

    public void setMainApp(MainApp mainApp) {
        this.mainApp = mainApp;

        //adiciona uma observable list
        filmeTable.setItems(mainApp.getfilmeDados());
    }

    private void showFilmeDetails(Filmes filme) {
        if (filme != null) {
            nomeLabel.setText(filme.getNome());
            categoriaLabel.setText(filme.getCategoria());
            duracaoLabel.setText(filme.getDuracao());
            protagonistaLabel.setText(filme.getProtagonista());
            classificacaoLabel.setText(filme.getClassificacao());
            diretorLabel.setText(filme.getDiretor());
            salaLabel.setText(filme.getSalaN());
        } else {
            nomeLabel.setText("");
            categoriaLabel.setText("");
            duracaoLabel.setText("");
            protagonistaLabel.setText("");
            classificacaoLabel.setText("");
            diretorLabel.setText("");
            salaLabel.setText("");
        }
    }

    @FXML
    private void handleDeletarFilme() {
        int selectedIndex = filmeTable.getSelectionModel().getSelectedIndex();
        if (selectedIndex >= 0) {
            filmeTable.getItems().remove(selectedIndex);
        } else {
            Alert alerta = new Alert(AlertType.WARNING);
            alerta.setTitle("Nenhum filme selecionado");
            alerta.setHeaderText("Nenhuma Selecao");
            alerta.setContentText("Por favor selecione um filme para deletar");
            alerta.showAndWait();
        }

    }

    @FXML
    private void handleNovoFilme() {
        Filmes tempFilme = new Filmes("Nome","Categoria");
        boolean clicado = mainApp.showEditarFilmeDialog(tempFilme);
        if (clicado) {
            mainApp.getfilmeDados().add(tempFilme);
        }

    }

    @FXML
    private void handleEditarFilme() {
        Filmes filmeSelecionado = filmeTable.getSelectionModel().getSelectedItem();
            if(filmeSelecionado != null) {
                boolean clicado = mainApp.showEditarFilmeDialog(filmeSelecionado);
                if(clicado) {
                    showFilmeDetails(filmeSelecionado);
                }
            }else {
                //se nada for selecionado
                Alert alerta = new Alert(AlertType.WARNING);
                alerta.initOwner(mainApp.getPrimaryStage());
                alerta.setTitle("Nenhuma selecao");
                alerta.setHeaderText("Nenhum filme selecionado");
                alerta.setContentText("Por favor selecione algum filme.");
                alerta.showAndWait();
            }
    }
}

There's a class called Sala, that's being imported into this controller, i could make it work with another class called Filmes, i don't know exactly why it's not working with the class Sala, here's the code in the class:

package projeto;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Sala {
    private boolean e3d;
    private int assentosMax;
    private int assentosDisp;
    private final StringProperty numeroProperty = new SimpleStringProperty();


    public Sala(boolean e3d, int assentosMax, int assentosDisp, String numero) {
        setNumero(numero);
        e3d = this.e3d;
        assentosMax = this.assentosMax;
        assentosDisp = this.assentosDisp;
    }
    public boolean isE3d() {
        return e3d;
    }
    public void setE3d(boolean e3d) {
        this.e3d = e3d;
    }
    public int getAssentosMax() {
        return assentosMax;
    }
    public void setAssentosMax(int assentosMax) {
        this.assentosMax = assentosMax;
    }
    public int getAssentosDisp() {
        return assentosDisp;
    }
    public void setAssentosDisp(int assentosDisp) {
        this.assentosDisp = assentosDisp;
    }
    public StringProperty numeroProperty() {
        return numeroProperty;
    }
    public final String getNumero() {
        return numeroProperty.get();
    }
    public final void setNumero(String numero) {
        numeroProperty().set(numero);
    }



}

Edit: Here's the error:

jun 07, 2018 3:18:37 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 9.0.1 by JavaFX runtime of version 8.0.171
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:498)
    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:498)
    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$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.Error: Unresolved compilation problem: 
    The method numeroProperty() is undefined for the type Filmes

    at projeto.resources.FilmeOverviewController.<init>(FilmeOverviewController.java:50)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at projeto.MainApp.showFilmeOverview(MainApp.java:59)
    at projeto.MainApp.start(MainApp.java:50)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
    ... 1 more
Exception running application projeto.MainApp

Thanks in advance!

更新时间:2023-03-21 14:03

最满意答案

这看起来像你期待表中的一种类型的对象,但提供另一种类型的对象。

你可以像这样定义你的表格:

@FXML
private TableColumn<Filmes, String> salaColumn;

您的单元格值工厂正试图获得以下内容:

salaColumn.setCellValueFactory(cellData -> cellData.getValue().numeroProperty());

numeroProperty位于Sala对象上,而不是Filmes对象。

尝试以下(尽管我不是100%确定的,因为您对电影的定义不在问题中):

salaColumn.setCellValueFactory(cellData -> cellData.getValue().getSala().numeroProperty());

另外 - 如果Sala对象有可能为null,则在尝试访问numeroProperty()之前,您需要检查该对象。


This looks like you are expecting one type of object in the table but providing another.

You define your table like this:

@FXML
private TableColumn<Filmes, String> salaColumn;

Your cell value factory is trying to get the following:

salaColumn.setCellValueFactory(cellData -> cellData.getValue().numeroProperty());

The numeroProperty is on the Sala object, not the Filmes Object.

Try the following (Though I am not 100% sure, since your definition of Filmes is not in the question):

salaColumn.setCellValueFactory(cellData -> cellData.getValue().getSala().numeroProperty());

Also - if there is a chance that the Sala object could be null, you'll want to check for that before trying to access the numeroProperty().

相关问答

更多

相关文章

更多

最新问答

更多
  • 在javascript中创建类以创建对象并在Java中创建类和对象之间的区别(Difference between creating a class in javascript to create an object and creating an class and object in Java)
  • Facebook API:将身份验证详细信息从Javascript SDK发送到PHP SDK(Facebook API: Send authentication detail from Javascript SDK to PHP SDK)
  • 如何停止队列动画jquery?(How can I stop queue animation jquery?)
  • 使用C#的井字游戏中的人工智能(Artificial Intelligence in Tic-Tac-Toe using C#)
  • 多少流量可以共享虚拟主机(对于Python Django站点)支持?(How Much Traffic Can Shared Web Hosting (for a Python Django site) support?)
  • 带有CIFilters的CAShapeLayer(CAShapeLayer with CIFilters)
  • 如何在Angular 2中读取JSON #text(How to read in Angular 2 the JSON #text)
  • 如何在xml中读取自闭标签的属性?(How to read self closing tag's attribute in xml?)
  • 无法使用http put将图像上传到亚马逊S3(Cannot upload image to amazon s3 using http put)
  • 文件结束无限循环(end of file infinite while-loop)
  • 在cpp的模板(template in cpp)
  • 在构建库时,clang和clang ++有什么区别?(What's the difference between clang and clang++ when building a library?)
  • ng类中的表达式(expression inside ng-class)
  • 在PHP中获取随机布尔值true / false(Get random boolean true/false in PHP)
  • 管道的高效分块用于严格的字节串(Efficient chunking of conduit for strict bytestring)
  • Python ternary_operator(如果其他标志做了其他操作,则执行其他操作)(Python ternary_operator (do-somthing if flag else do-another))
  • Sencha Touch面具发布(Sencha Touch mask ondisclosure)
  • 验证脚本上的通知[重复](Notices on validation script [duplicate])
  • 朋友功能(friend function)
  • 基于角坐标平移和变换平面几何(Translate and transform plane geometry based on corner coordinates)
  • Rails:'如果在本地运行'条件javascript标记包括(Rails: 'if running locally' conditional javascript tag include)
  • 解压文件(Unzipping files)
  • 使用ui-router以角度加载变量状态(loading in variable states with ui-router in angular)
  • 创建Azure云服务需要多长时间?(how long does it take to create an Azure Cloud Service? How to view log information?)
  • 指向整数的指针数组(Array of pointers to integers)
  • Laravel服务提供商没有看到我的包的主要类(Laravel service provider does not see the main class of my package)
  • 这个关于VSS / RSS / PSS / USS的解释是否准确?(Is this explanation about VSS/RSS/PSS/USS accurate?)
  • 在Django-Admin中通过row-id排序显示项目(Ordering the display items by row-id in Django-Admin)
  • 如何使用cythonize启用`--embed`?(How to enable `--embed` with cythonize?)
  • 用于将文本多行设置的Excel脚本(Excel script for ereasing text multiple rows)