Wtf is ButtonNull and why does it exist?

I recently had a typo on the id of a button - but it took me a while to track this down as the findNiftyControl was returning a Button even though no matching one returned - and it seems that this Button was an instance of something called ButtonNull which just silently ignores every method call to it!

Surely if the control I’m looking for doesn’t exist I should get a null back, not an object that tries to pretend to be what I looked for but in fact isn’t it?

Interesting link thanks, i’m not convinced though.especially as their example is bogus as no directory listing gives null for an empty list…they return an empty list. A null return might be used if folder doesn’t exist.

From that Wikipedia article:

This pattern should be used carefully as it can make errors/bugs appear as normal program execution.

Other links:

Yeeh, I get the concept and I’m a big fan of Collections.emptyList etc which is used in a similar way.

I’m still not seeing the benefits in this case but at least I can sort of see the logic behind it now…it really needs documenting better though. I might even submit a patch with some extra javadoc on the control finding method!

@zarch said:I might even submit a patch with some extra javadoc on the control finding method!

The best gift you can give to any open source project


I’ve applied your patch and extended it a bit. Thanks!

Here is the additional JavaDoc comment I’ve added that should explain the NullObject a bit more:

“… If no matching control is found then a Null version of the control

will be returned to prevent NullPointerExceptions if you use it directly like:

screen.findNiftyControl(…).doStuff(). There is no need to check the return value

or catch exceptions. While testing your GUI please take a look at the console log

so that you see the error message that is written in case a control can’t be found

(see also for details).”

The message you get in the log is this one:

[java]log.warning(“missing element/control with id [” + elementName + “] for requested control class [” + requestedControlClass.getName() + “]”);[/java]

and with Nifty 1.3.2 configuring the jdk14 logging should work now finally correctly as well :wink: see this link for details:

1 Like

Thanks, that’s much clearer :slight_smile: