简体   繁体   English

JavaFx 13 - TableView 垂直滚动条处理程序返回 NullPointerException

[英]JavaFx 13 - TableView Vertical ScrollBar handler returns NullPointerException

I need to handle JavaFx13 scroll to bottom event, but this code:我需要处理 JavaFx13 滚动到底部事件,但此代码:

   @Override
   public void initialize(URL location, ResourceBundle resources) {
     // ...
     // ScrollBar verticalBar = (ScrollBar) this.emailsTable.lookupAll(".scroll-bar");
        ScrollBar verticalBar = (ScrollBar) this.emailsTable.lookup(".scroll-bar:vertical");
        verticalBar.valueProperty().addListener((obs, oldValue, newValue) -> { // <-- Line 49
             // if (verticalBar.getOrientation() != Orientation.VERTICAL) return;
            if (newValue.doubleValue() >= verticalBar.getMax()) {
                System.out.println("BOTTOM!");
            }
        });
     // ...
   }

...returns that error at FXML load: ...在 FXML 加载时返回该错误:

Caused by: java.lang.NullPointerException
at it.unito.prog.views.MainView.initialize(MainView.java:49)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
... 19 more

You can scroll using tableView.scrollTo() to scroll to either an index or a specific item.您可以使用tableView.scrollTo()滚动到索引或特定项目。

To be notified that a scroll has occurred, you can use tableView.setOnScrollTo() .要收到滚动发生的通知,您可以使用tableView.setOnScrollTo()

Don't use a lookup for this task不要为此任务使用查找

The scroll bar is only shown as needed.滚动条仅在需要时显示。 It may or may not be there when you look it up.当您查找它时,它可能存在也可能不存在。

If you try to lookup() the scroll bar before you add items to the table view, or before you add the table view to a scene, or before the scene has undergone a rendering pass, a scroll bar is almost certain to not be there.如果您在向 table view 添加项目之前,或者在将 table view 添加到场景之前,或者在场景经过渲染过程之前尝试lookup()滚动条,则几乎可以肯定滚动条不存在. If the scroll bar is not there when you try to look it up then the lookup method will return a null value (which is what you are seeing).如果在您尝试查找时滚动条不存在,则查找方法将返回一个空值(这就是您所看到的)。 Even if a scroll bar is there when you first look it up, it may be subsequently removed and a new one added as needed, so your original reference will end up becoming invalid.即使您第一次查找滚动条时有滚动条,它也可能随后被删除并根据需要添加一个新的滚动条,因此您的原始引用最终将变得无效。 So I don't recommend your lookup based approach.所以我不推荐你的基于查找的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM