简体   繁体   English

为什么ListChangeBuilder抛出NullPointerException?

[英]Why is ListChangeBuilder throwing a NullPointerException?

I'm trying to figure out why I'm getting this error: 我试图弄清楚为什么会出现此错误:

java.lang.NullPointerException: null
    at javafx.collections.ListChangeBuilder.findSubChange(ListChangeBuilder.java:68)
    at javafx.collections.ListChangeBuilder.insertAdd(ListChangeBuilder.java:127)
    at javafx.collections.ListChangeBuilder.nextAdd(ListChangeBuilder.java:254)
    at javafx.collections.ObservableListBase.nextAdd(ObservableListBase.java:179)
    at javafx.collections.ModifiableObservableListBase.add(ModifiableObservableListBase.java:153)
    at java.util.AbstractList.add(AbstractList.java:108)
    at tech.dashman.dashman.RendererApp$1.handle(RendererApp.java:72)
    at tech.dashman.common.pubnub.NewScreenshotsToRender.handle(NewScreenshotsToRender.java:17)
    at tech.dashman.dashman.PubNubMessageHandler.lambda$message$0(PubNubMessageHandler.java:79)
    at java.lang.Thread.run(Thread.java:748)

It happened twice on my application but I don't know how to reproduce it. 它在我的应用程序上发生了两次,但我不知道如何重现它。 The line in my code that triggers the error is this: 我的代码中触发错误的行是这样的:

getActivity().add(new ActivityEntry("..."));

ActivityEntry is a simple class with two fields. ActivityEntry是具有两个字段的简单类。 I can post the code if necessary but I think it's irrelevant. 如有必要,我可以发布代码,但我认为这无关紧要。 activity , what getActivity() returns, is a field defined like this: activitygetActivity()返回的,是一个定义如下的字段:

private ObservableList<ActivityEntry> activity = FXCollections.observableArrayList();

The method in which the actual exception is happening looks like this: 发生实际异常的方法如下所示:

private int findSubChange(int idx, final List<ListChangeBuilder.SubChange<E>> list) {
    int from = 0;
    int to = list.size() - 1;

    while (from <= to) {
        int changeIdx  = (from + to) / 2;
        ListChangeBuilder.SubChange<E> change = list.get(changeIdx);

        if (idx >= change.to) {
            from = changeIdx + 1;
        } else if (idx < change.from) {
            to = changeIdx - 1;
        } else {
            return changeIdx;
        }
    }
    return ~from;
}

Line 68, the one raising the exception is: 68行,引发异常的是:

if (idx >= change.to) {

The only thing I can think of, other than a bug in ListChangeBuilder , is that it was a race condition and two threads modified the ObservableList at the same time. 除了ListChangeBuilder的错误外,我唯一能想到的是这是一个竞争条件,两个线程同时修改了ObservableList Does this look like that? 看起来像那样吗? Is ObservableList not thread safe? ObservableList不是线程安全的吗?

This looks like a multi-threading issue, so, for now, my solution is to define the list this way: 这看起来像一个多线程问题,因此,目前,我的解决方案是通过这种方式定义列表:

private ObservableList<ActivityEntry> activity = FXCollections.synchronizedObservableList(FXCollections.observableArrayList());

Since multi-threading issues are so hard to reproduce and debug, I don't know whether this really solves the problem. 由于多线程问题很难重现和调试,因此我不知道这是否真的可以解决问题。 I'm guessing if it doesn't I'll notice it again in production. 我猜如果不是,我会在生产中再次注意到它。

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

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