Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NullPointer exception caused by incorrect call of onViewDetached #1851

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public abstract class View<R extends Region, M extends Model, C extends Controll
protected final C controller;
private final ChangeListener<Scene> sceneChangeListener;
private ChangeListener<Window> windowChangeListener;
private boolean isViewAttached;

public View(R root, M model, C controller) {
checkNotNull(root, "Root must not be null");
Expand All @@ -55,7 +56,8 @@ private void handleSceneChange(Scene oldValue, Scene newScene) {
} else {
// For overlays, we need to wait until window is available
windowChangeListener = (observable, oldWindow, newWindow) -> {
if (newWindow != null) {
// As we get called with a delay it might be that the root scene has already changed to null.
if (newWindow != null && root.getScene() != null) {
onViewAttachedPrivate();
} else {
onViewDetachedPrivate();
Expand All @@ -82,15 +84,22 @@ public R getRoot() {
}

private void onViewDetachedPrivate() {
// In case we have an overlay we might get called before the onViewAttachedPrivate was called in the windowChangeListener
// We avoid that call as it could lead to null pointers in the onViewDetached methods.
if (!isViewAttached) {
return;
}
controller.onDeactivateInternal();
onViewDetachedInternal();
isViewAttached = false;
}

private void onViewAttachedPrivate() {
// View is listening on model changes triggered by controller, so we call controller first, so that view has
// correct state.
controller.onActivateInternal();
onViewAttachedInternal();
isViewAttached = true;
}

// The internal methods should be only used by framework classes (e.g. TabView)
Expand Down
Loading