Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Invoking JavaFX functions that don't exist


 

What happens when you try to execute the following Java code fragment?

String s = null;
System.out.println (s.length ());

It's not hard to figure out that a NullPointerException object is thrown. You cannot invoke a method via the null reference.

However, you can get away with something similar in JavaFX Script, which the following code fragment demonstrates:

var x: function (): Void;
println (x); // Output: null
x ();
x = null;
x ();
x = function (): Void { println ("x() invoked") }
x () // Output: x() invoked

The code fragment initially creates variable x of type function (): Void. Furthermore, this variable is initialized to null, which the subsequent println (x); expression proves.

Notice the first x (); invocation. You might expect that the JavaFX runtime would throw a NullPointerException, but this doesn't happen. In fact, the invocation is ignored.

To make this more explicit, the code fragment assigns null to x and once more executes x ();. Same result.

Finally, the code fragment assigns an anonymous function to x and executes x (), which results in the function being invoked and outputting x() invoked.

This "ignore a function invocation when the function variable contains null" behavior is present in JavaFX 1.0 and 1.1, and appears to be a useful feature for making code more robust. However, it can also mask bugs when a function must be executed but never is because its function variable contains null.

Do you think that a NullPointerException should be thrown in this situation, or is the ignore behavior okay with you?

I'm a strong proponent of

I'm a strong proponent of enforcing strict rules; first and above all code must execute predictably. Remember what we have gotten "ourselves" into because "we" allowed IE to render all kinds of garbage HTML correctly?

Ignoring a NPE is a great way to mask fundamental errors in the code. So I personally want that NPE.

Deciding what projects will

Deciding what projects will live and what projects will die once Java has "Property of Oracle" stamped on its behind is, of course, Oracle's job; but predicting those outcomes is good clean fun for everyone, until then! I've already discussed a couple of projects people have been anxious about -- Glassfish and JavaFX; in the last few days, there's been a few peeps about JRuby and Swing.

Save NPEs!

Horrible idea to ignore NPEs!!! Just ask Flex developers :) It's absolutely atrocious to try and find an 'invisible' NPE-inspired bug in Flex...

Please, for the love of all that's good do NOT silence NPEs!

What?

What do you think of JavaFX script as a DSL for user interfaces?

I would expect that a

I would expect that a NullPointerException should be thrown in this situation as that would allow the JAVA compiler to spot and debug the code error. This supports what the JAVA language is known for - robust code and ability to find & fix errors fast!

"hi i created image

"hi i created image transition demo in JavaFX u can see it here it take some time to load because i upload html file on one server and JNLP and jar file on other server on my server it does not allow JNLP files."

Farrukh Obaid,

Sweet! Have you thought about putting it on jfxstudio.org and showing the source code?

All three values are the

All three values are the same? So adding bound even made things worse! How is it possible? Well, bound makes JFX not only can recognize which variables affect return values, but also which function parameters doesn't affect it. So now JFX is aware that p doesn't affect return value of the function and it calls the function only on the first time. Then it takes value from cache, regardless parameter has changed or not.

Horrible idea to ignore

Horrible idea to ignore NPEs!!! Just ask Flex developers :) It's absolutely atrocious to try and find an 'invisible' NPE-inspired bug in Flex...

Please, for the love of all that's good do NOT silence NPEs!

null pointer exception

When you see a null pointer exception. You should go to the line specified in the error and check if there might be any null references there. To do that you can add basic "if statements" for your object references used around that line (just like goldhouse did for f variable in the above code.). Once you found the null reference variable, you can fix the problem by changing the code there. This step is application specific: you might need to make sure that no null reference is there or you can allow null references but control the program flow for not getting a null pointer exception.

Best solution

I agree this is the best solution to solve it. When you are programming is always better to fix the problem than to mask it.

JavaFX functions

Deciding what projects will live and what projects will die once Java has "Property of Oracle" stamped on its behind is.

I'm a strong proponent of enforcing strict rules; first and above all code must execute predictably

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br /> <br> <strike>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Just checking to see if you're an actual person rather than a spammer. Sorry for the inconvenience.