Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8357620

Expression binding error with forward reference

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      JDK23.0.2 on MacOS 15.5 (aarch64)

      A DESCRIPTION OF THE PROBLEM :
      If an object property is bound to another object property with an FXML binding expression (e.g. ${textField1.length*2}), and the source object comes later in the XML than the target object, a NullPointerException occurs in FXMLLoader.

      If the binding is a simple 1:1 binding (e.g. ${textField1.length}) there is no error.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Set up a project with the test case code below and run it.



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Should show a stage where the Label text is set to 2 x the length of the TextField, and is updated as text is typed into the field.
      ACTUAL -
      A null pointer exception, with this line:
      Caused by: java.lang.NullPointerException: Cannot invoke "java.lang.Number.longValue()" because "<parameter1>" is null
      at javafx.fxml@24.0.1/com.sun.javafx.fxml.expression.Expression.lambda$multiply$2(Expression.java:932)


      Full trace:

      Exception in Application start method
      Exception in thread "main" java.lang.reflect.InvocationTargetException
      at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
      at java.base/java.lang.reflect.Method.invoke(Method.java:580)
      at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1164)
      Caused by: java.lang.RuntimeException: Exception in Application start method
      at javafx.graphics@24.0.1/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
      at javafx.graphics@24.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$0(LauncherImpl.java:197)
      at java.base/java.lang.Thread.run(Thread.java:1575)
      Caused by: javafx.fxml.LoadException:
      /Users/peterhull/Projects/jfx-bug-proj/target/classes/com/mycompany/jfxbugproj/main.fxml:5

      at javafx.fxml@24.0.1/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2692)
      at javafx.fxml@24.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2670)
      at javafx.fxml@24.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2533)
      at javafx.fxml@24.0.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2503)
      at com.mycompany.jfxbugproj.App.start(App.java:21)
      at javafx.graphics@24.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$7(LauncherImpl.java:840)
      at javafx.graphics@24.0.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$5(PlatformImpl.java:444)
      at javafx.graphics@24.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$4(PlatformImpl.java:419)
      at javafx.graphics@24.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
      Caused by: java.lang.NullPointerException: Cannot invoke "java.lang.Number.longValue()" because "<parameter1>" is null
      at javafx.fxml@24.0.1/com.sun.javafx.fxml.expression.Expression.lambda$multiply$2(Expression.java:932)
      at javafx.fxml@24.0.1/com.sun.javafx.fxml.expression.BinaryExpression.evaluate(BinaryExpression.java:55)
      at javafx.fxml@24.0.1/com.sun.javafx.fxml.expression.ExpressionValue.getValue(ExpressionValue.java:191)
      at javafx.base@24.0.1/com.sun.javafx.binding.ExpressionHelper.addListener(ExpressionHelper.java:64)
      at javafx.base@24.0.1/javafx.beans.value.ObservableValueBase.addListener(ObservableValueBase.java:57)
      at javafx.fxml@24.0.1/com.sun.javafx.fxml.expression.ExpressionValue.addListener(ExpressionValue.java:200)
      at javafx.base@24.0.1/javafx.beans.property.StringPropertyBase.bind(StringPropertyBase.java:171)
      at javafx.fxml@24.0.1/javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:318)
      at javafx.fxml@24.0.1/javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:235)
      at javafx.fxml@24.0.1/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:767)
      at javafx.fxml@24.0.1/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2939)
      at javafx.fxml@24.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2624)
      ... 7 more


      ---------- BEGIN SOURCE ----------
      Example FXML:

      <?xml version="1.0" encoding="UTF-8"?>
      <?import javafx.scene.layout.*?>
      <?import javafx.scene.control.*?>
      <VBox xmlns:fx="http://javafx.com/fxml">
          <Label text="${textField1.length*2}" />
          <TextField fx:id="textField1" />
      </VBox>

      Example Java:

      package com.mycompany.jfxbugproj;

      import javafx.fxml.FXMLLoader;
      import javafx.scene.Scene;
      import javafx.scene.layout.Pane;
      import javafx.stage.Stage;

      public class App extends javafx.application.Application {

          @Override
          public void start(Stage stage) throws Exception {
              FXMLLoader loader = new FXMLLoader(App.class.getResource("main.fxml"));
              Pane root = loader.load();
              Scene scene = new Scene(root);
              stage.setScene(scene);
              stage.show();
          }
      }
      ---------- END SOURCE ----------

            arapte Ambarish Rapte
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: