Tableview getItems.AddAll导致java.lang.reflect.InvocationTargetException

时间:2017-11-05 01:17:17

标签: java javafx javafx-8

更新:原因是tblTimeDataClock为null。任何想法为什么?

我在调用getItems()。AddAll()方法时得到一个Tableview getItems.AddAll导致java.lang.reflect.InvocationTargetException。我试过跟踪互联网上的各种例子,但似乎没有什么工作。例外情况发生在tblTimeDataClock.getItems()。addAll(data)调用。

我确实在新的PropertyValue调用中看到一个警告,说明新表达式中的Redundant Type参数。不确定这与它有什么关系,

我做错了什么?

我把这个简短的例子放在一起。

@FXML
private TableView<Test> tblTimeDataClock;

@FXML
private TableColumn<Test, String> colTimeSeconds;

@FXML
private TableColumn<Test, String> colDataVolts;

@FXML
private TableColumn<Test, String> colClockVolts;   


@Override
public void initialize(URL url, ResourceBundle rb) {

    listTimeDataClockMeasurement = new <Test>ArrayList();

    colTimeSeconds.setCellValueFactory(new PropertyValueFactory<Test, String>("n1"));
    colDataVolts.setCellValueFactory(new PropertyValueFactory<Test, String>("n2"));
    colDataVolts.setCellValueFactory(new PropertyValueFactory<Test, String>("n3"));

    ObservableList<Test> data = FXCollections.observableArrayList();      

    tblTimeDataClock.getItems().addAll(data);


}

测试类

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

  public class Test {

private final SimpleStringProperty n1;
private final SimpleStringProperty n2;
private final SimpleStringProperty n3;

public String getN1() {
    return n1.get();
}

public String getN2() {
    return n2.get();
}


public String getN3() {
    return n3.get();
}

public Test(String n1, String n2, String n3) {
    this.n1 = new SimpleStringProperty(n1);
    this.n2 = new SimpleStringProperty(n2);
    this.n3 = new SimpleStringProperty(n3);
}

}

根据要求添加FXML

                   <content>
                        <TableView fx:id="tblDataClock" prefHeight="200.0" prefWidth="200.0">
                          <columns>
                            <TableColumn fx:id="colTimeSeconds" prefWidth="116.0" text="Time" />
                            <TableColumn id="colData" fx:id="colDataVolts" prefWidth="127.0" text="Data" />
                              <TableColumn id="colClock" fx:id="colClockVolts" prefWidth="137.0" text="Clock" />
                          </columns>
                        </TableView>
                     </content>

看起来原因可能是java.lang.NullPointerException     at serialdecoder.SerialDecoderController.initialize(SerialDecoderController.java:60)

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$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
file:/E:/Data/Java/SerialDecoder/dist/run67790734/SerialDecoder.jar!/serialdecoder/SerialDecoder.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at serialdecoder.SerialDecoder.start(SerialDecoder.java:22)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
    ... 1 more
Caused by: java.lang.NullPointerException
    at serialdecoder.SerialDecoderController.initialize(SerialDecoderController.java:60)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 17 more
Exception running application serialdecoder.SerialDecoder    

1 个答案:

答案 0 :(得分:0)

发现它!我的TableView ID在我的FXML文件中不正确。 :(