使用datafx从其余脚本读取json。
下面是代码

    @FXML
private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    label.setText("Hello World!");
    JsonConverter jsonConvert = new JsonConverter("jsonCase", CaseTable.class);
    RestSource test = RestSourceBuilder.create()
                      .converter(jsonConvert)
                      .host("http://devel.com.au")
                      .path("cgi-bin").path("ssinternalweb-jcgi.sh")
                      .queryParam("Program", "appfx/CaseTest.p")
                      .queryParam("ProcName", "FXRequest")
                      .queryParam("TestParam", "aValue").build();
    ListDataProvider<CaseTable> ldp = ListDataProviderBuilder.create()
            .dataReader(test)
            .build();
    Worker<ObservableList<CaseTable>>retrieve = ldp.retrieve();
    lvCases.setItems(retrieve.getValue());
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    lvCases.setCellFactory(l -> new CaseTableCell());
    lvCases.getItems().add(new CaseTable(999, "Loading..."));
}


当工作人员开始处理结果时,出现以下错误消息

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at org.datafx.provider.PublishingTask$1.run(PublishingTask.java:41)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1175034525.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/501263526.run(Unknown Source)
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$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/96639997.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)


我在类中尝试创建的断点似乎正在接收正确的值。唯一的问题不在列表视图的屏幕上显示。

private ListView<CaseTable> lvCases;

最佳答案

如我的评论之一所述,应该设置结果列表,并且不会收到此错误消息。这是一条不同的路线,这就是为什么思想不相关但是却是完全相同的问题。
以下是现在可以使用的更新代码。

    @FXML
private void handleButtonAction(ActionEvent event) {
    JsonConverter jsonConvert = new JsonConverter("jsonCase", CaseTable.class);
    RestSource test = RestSourceBuilder.create()
                      .converter(jsonConvert)
                      .host("http://devel.com.au")
                      .path("cgi-bin").path("ssinternalweb-jcgi.sh")
                      .queryParam("Program", "appfx/CaseTest.p")
                      .queryParam("ProcName", "FXRequest")
                      .queryParam("TestParam", "aValue").build();
    lvCases.getItems().removeAll(lvCases.getItems());
    ListDataProvider<CaseTable> ldp = ListDataProviderBuilder.create()
            .dataReader(test)
            .resultList(lvCases.getItems())
            .build();
    Worker<ObservableList<CaseTable>>retrieve = ldp.retrieve();
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    lvCases.setCellFactory(l -> new CaseTableCell());
    lvCases.getItems().add(new CaseTable(999, "Loading..."));
}


详情请参阅

https://bitbucket.org/datafx/datafx/issue/9/npe-when-resultlist-not-specified

09-11 18:55