JavaFX Can not retrieve property

lusitano

Aktives Mitglied
Moin Leute.
Meines Erachtens müsste meine Tableview funktionieren. Tut sie aber nicht.
Ich mag bestimmt meinen Feher übersehen. Falls jemand mir ihn mal zeigen könnte. Danke schon mal
DataTableModel:

Java:
public class DataTableModel {

    private StringProperty vorname;
    private StringProperty nachname;
    private StringProperty benutzerRolle;
    private BooleanProperty schreiben;
    private BooleanProperty lesen;
    

    public DataTableModel(String lvorname, String lnachname, String lbenutzerRolle, boolean lschreiben, boolean llesen) {
        super();
        this.vorname = new SimpleStringProperty(lvorname);
        this.nachname = new SimpleStringProperty(lnachname);
    }

    public DataTableModel() {

    }

    
    public String getVorname() {
        return vorname.get();
    }
    
    
    public void setVorname(String lvorname) {
        vorname.set(lvorname);
    }
    
    public String getNachname() {
        return nachname.get();
    }
    
    public void setNachname(String lnachname) {
        nachname.set(lnachname);
    }

Controller:
Java:
public class MainWindowController implements Initializable {


    @FXML
    private TableView<DataTableModel> tvView;

    @FXML
    private TableColumn<DataTableModel, String> colVorname;

    @FXML
    private TableColumn<DataTableModel, String>  colNachname;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ObservableList<DataTableModel> liste = FXCollections.observableArrayList();
        DataTableModel datamodel = new DataTableModel();
        
        cbBenutzerrolle.setItems(cbList);
        
        colVorname.setCellValueFactory(new PropertyValueFactory<DataTableModel, String>("vorname"));

        colVorname.setCellValueFactory(new PropertyValueFactory<DataTableModel, String>("nachname"));

        liste.add(getDataTableModel(datamodel));
        
        tvView.setItems(liste);
        
    }

und die Exception

Java:
Sep. 25, 2019 9:00:05 VORM. javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'vorname' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@656d84c3 with provided class type: class objekte.DataTableModel
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.base/com.sun.javafx.property.PropertyReference.get(PropertyReference.java:176)
    at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.getCellDataReflectively(PropertyValueFactory.java:184)
    at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:154)
    at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:133)
    at javafx.controls/javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:593)
    at javafx.controls/javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:578)
    at javafx.controls/javafx.scene.control.TableCell.updateItem(TableCell.java:646)
    at javafx.controls/javafx.scene.control.TableCell.indexChanged(TableCell.java:469)
    at javafx.controls/javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:120)
    at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:539)
    at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.<init>(TableRowSkinBase.java:159)
    at javafx.controls/javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:89)
    at javafx.controls/javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:213)
    at javafx.controls/javafx.scene.control.Control.doProcessCSS(Control.java:897)
    at javafx.controls/javafx.scene.control.Control$1.doProcessCSS(Control.java:89)
    at javafx.controls/com.sun.javafx.scene.control.ControlHelper.processCSSImpl(ControlHelper.java:67)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:145)
    at javafx.graphics/javafx.scene.Node.processCSS(Node.java:9540)
    at javafx.graphics/javafx.scene.Node.applyCss(Node.java:9627)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1715)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1692)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1818)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2721)
    at javafx.controls/javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1245)
    at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1206)
    at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1213)
    at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1213)
    at javafx.graphics/javafx.scene.Scene.doLayoutPass(Scene.java:576)
    at javafx.graphics/javafx.scene.Scene.preferredSize(Scene.java:1748)
    at javafx.graphics/javafx.scene.Scene$2.preferredSize(Scene.java:393)
    at javafx.graphics/com.sun.javafx.scene.SceneHelper.preferredSize(SceneHelper.java:66)
    at javafx.graphics/javafx.stage.Window$12.invalidated(Window.java:1086)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
    at javafx.graphics/javafx.stage.Window.setShowing(Window.java:1174)
    at javafx.graphics/javafx.stage.Window.show(Window.java:1189)
    at javafx.graphics/javafx.stage.Stage.show(Stage.java:273)
    at entry.StartapplicationController.btnLogAction(StartapplicationController.java:52)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8890)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3862)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:411)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.base/com.sun.javafx.property.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.base/com.sun.javafx.property.PropertyReference.get(PropertyReference.java:174)
    ... 94 more
Caused by: java.lang.NullPointerException
    at objekte.DataTableModel.getVorname(DataTableModel.java:32)
    ... 105 more
 

Robat

Top Contributor
Die PropertyValueFactory sucht über Reflection nach einer Methode mit dem folgenden Namen <name>Property. Der String, der du der PropertyValueFactory übergibst, ist dabei <name>. Die Factory erwartet also bei dir 2 Methoden in deiner Model Klasse
Java:
public StringProperty vornameProperty() {
    return vorname;
}
public StringProperty nachnameProperty() {
   return nachname;
}
 

looparda

Top Contributor
public DataTableModel() {
// Initialisierung fehlt
}
public String getVorname() { return vorname.get(); }
Liegt vermutlich am leeren Konstruktor, der dir die Properties im Model nicht initialisiert.
liste.add(getDataTableModel(datamodel));
Was passiert da drin?
colVorname.setCellValueFactory(new PropertyValueFactory<DataTableModel, String>("vorname"));
colVorname.setCellValueFactory(new PropertyValueFactory<DataTableModel, String>("nachname"));
Copy Paste Fehler?

Im anderen Beitrag hast du geschrieben, dass du extra Properties vermeiden möchtest, wenn ich mich richtig erinnere. Das ist mit der CellValueFactory und PropertyValueFactory möglich - sofern du keine Properties benutzt und dich an die Namenskonventionen für Getter hälst findet diese Implementierung die Werte. Allerdings ist die Klasse, wie der Name der Factory "PropertyValue" sagt eher für Properties ausgelegt. Wenn du kein Binding zwischen View und Modell brauchst, also nur lesen möchtest, dann könnte dies aber ausreichen.
 

lusitano

Aktives Mitglied
12421

Das hier:

Java:
    liste.add(getDataTableModel(datamodel));
        
        tvView.setItems(liste);
        
    }

    public DataTableModel getDataTableModel(DataTableModel dtm) {
        return dtm;
    }


mit derMethode getDataTableModel() hole ich in der Klasse Helpers meine erzeugten Objekte.


Code:
DataTableModel dtm = new DataTableModel(vorname, nachname, benutzerRolle, schreiben, lesen);
                
                MainWindowController mvc = new MainWindowController();
                mvc.getDataTableModel(dtm);




Denke so müsste das richtig sein.
Falls nicht gerne anmerken. (lerne gerne dazu).

aber auch dir ein herzliches Dankeschön für deine Hilfe.
 

looparda

Top Contributor
mit derMethode getDataTableModel() hole ich in der Klasse Helpers meine erzeugten Objekte.
Versteh ich leider nicht. Du bist im MainWindowController und rufst deine Helfer-Klasse auf, die dir eine neue Instanz vom MainWindowController erstellt, von der du dir das TableModel holst
public DataTableModel getDataTableModel(DataTableModel dtm) { return dtm; }
indem du das TableModel hineingibst und das selbe wieder herausgibst (???).

Vielleicht kannst du kurz erklären, was du versuchst dort zu tun und man findet einen einfacheren Weg.
 

lusitano

Aktives Mitglied
Versteh ich leider nicht.

Es ist auch für mich sehr verwirrend. #nochZuFrühFürBier?
Ja. Ich hatte versucht ein Objekt des DataTableModel in den Maincontroller zu übergeben.
und dort in die Initzialize zu bekommen.


In Zwischenzeit habe ich aber die TableView mal komplett auseinander genommen und frisch aufgebaut. Zumindest die funktioniert.

MainController:
Java:
package entry;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
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;
import javafx.scene.layout.AnchorPane;
import objekte.DataTableModel;

public class StartapplicationController implements Initializable {

    DataTableModel dtmDataTableModel;
    ObservableList<DataTableModel> dataList = FXCollections.observableArrayList();
    
    @FXML
    private AnchorPane startMain;

    @FXML
    private Button btnSpeichern;

    @FXML
    private TextField tfVorname;

    @FXML
    private TextField tfNachname;

    @FXML
    private TableView<DataTableModel> tvView;

    @FXML
    private TableColumn<DataTableModel, String> colVorname;

    @FXML
    private TableColumn<DataTableModel, String> colNachname;

    @FXML
    void btnSpeichernAction(ActionEvent event) {
        
        String vorname = tfVorname.getText().trim();
        String nachname = tfNachname.getText().trim();
        
        dtmDataTableModel = new DataTableModel(vorname, nachname);
        
        dataList.add(dtmDataTableModel);
        
        tfVorname.clear();
        tfNachname.clear();
    }

    @Override
        public void initialize(URL location, ResourceBundle resources) {
        
            
            
            colVorname.setCellValueFactory(new PropertyValueFactory<DataTableModel, String>("vorname"));
            colNachname.setCellValueFactory(new PropertyValueFactory<DataTableModel, String>("nachname"));
            
            
            
            tvView.setItems(dataList);
            
            
        }

    

}

DataTableModel:
Java:
package objekte;

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

public class DataTableModel {

    private StringProperty vorname;
    private StringProperty nachname;
    
    

    public DataTableModel(String lvorname, String lnachname) {
        super();
        this.vorname = new SimpleStringProperty(lvorname);
        this.nachname = new SimpleStringProperty(lnachname);
        
    }

    public DataTableModel() {

    }

    
    public String getVorname() {
        return vorname.get();
    }
    
    
    public void setVorname(String lvorname) {
        vorname.set(lvorname);
    }
    
    public String getNachname() {
        return nachname.get();
    }
    
    public void setNachname(String lnachname) {
        nachname.set(lnachname);
    }
   
    
}
 

Ähnliche Java Themen

Neue Themen


Oben