Skip to content

Fixing tests where n might potentially be NULL#1967

Merged
petebankhead merged 2 commits intoqupath:mainfrom
zindy:brightness-contrast-nullchecks
Aug 8, 2025
Merged

Fixing tests where n might potentially be NULL#1967
petebankhead merged 2 commits intoqupath:mainfrom
zindy:brightness-contrast-nullchecks

Conversation

@zindy
Copy link
Contributor

@zindy zindy commented Aug 8, 2025

Hello,

This is to solve a very unlikely edge case where the variable n (the new value of an observed property when it changes) is NULL in two tests (as far as I can tell) in the Brightness & Contrast dialog. This can happen because the dialog can be opened even if no image is available in the current viewer.

Apparently a classic autounboxing null problem (?) in Java: When n is null, this causes a NullPointerException because Java tries to invoke n.booleanValue() on a null reference.

I only noticed this because my autodock script crashed with

[JavaFX Application Thread]	ERROR	qupath.lib.gui.QuPathUncaughtExceptionHandler	Cannot invoke "java.lang.Boolean.booleanValue()" because "n" is null	java.lang.NullPointerException: Cannot invoke "java.lang.Boolean.booleanValue()" because "n" is null
	at qupath.lib.gui.commands.display.BrightnessContrastChannelPane.lambda$new$0(BrightnessContrastChannelPane.java:149)
	at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:192)
	at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:91)
	at javafx.beans.binding.ObjectBinding.invalidate(ObjectBinding.java:192)

Cheers,
Egor

@petebankhead
Copy link
Member

Would orElse(Boolean) solve the problem? Something like

    table.sceneProperty().flatMap(Scene::windowProperty).flatMap(Window::showingProperty).orElse(Boolean.FALSE).addListener((v, o, n) -> {
            if (n) updateShowTableColumnHeader();
        });

I didn't actually know of the orElse method until just now, but it feels like a better way to handle potential nulls here... if it works for you.

@zindy
Copy link
Contributor Author

zindy commented Aug 8, 2025

I will not even pretend to be able to advise you one way or the other. But I can confirm your fix also works. The two tests would then be:

table.sceneProperty().flatMap(Scene::windowProperty).flatMap(Window::showingProperty).orElse(Boolean.FALSE).addListener((v, o, n) -> {
            if (n) updateShowTableColumnHeader();
        });

and (in tryToKeepSearchText()):

comboSettings.sceneProperty().flatMap(Scene::windowProperty).flatMap(Window::showingProperty).orElse(Boolean.FALSE).addListener((v, o, n) -> {
            if (n && defaultName == null)
                handleComboShowing();
        });

@petebankhead
Copy link
Member

Ah good, could you update the PR for that please?

I couldn't see a way to trigger the issue in QuPath without your script, so I think under 'normal' circumstances the null check might not be needed. I think the orElse is less likely to be removed by some future over-enthusiastic attempt at code cleanup.

@zindy
Copy link
Contributor Author

zindy commented Aug 8, 2025

I couldn't see a way to trigger the issue in QuPath without your script, so I think under 'normal' circumstances the null check might not be needed.

I'm really sorry about all the fuss. Of course it would be one of my scripts that causes such a niche issue ;-)

Ah good, could you update the PR for that please?

Done!

@petebankhead
Copy link
Member

Thanks!

@petebankhead petebankhead merged commit 15d6439 into qupath:main Aug 8, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants