Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Why does this select binding throw a NullPointerException?

ryan29Jan 24 2014 — edited Apr 8 2014

This should be pretty simple.  The javadoc for Bindings#selectString says:

Creates a binding used to get a member, such as a.b.c. The value of the binding will be c, or "" if c could not be reached (due to b not having a c property, b being null, or c not being a String etc.).

Running this example:

import javafx.beans.binding.Bindings;

import javafx.beans.binding.StringBinding;

import javafx.beans.property.*;

public class SelectBindingExample {

    public static void main(String[] args) {

        ParentModel parentModel = new ParentModel();

        StringBinding bindingA = createBinding(parentModel);

        String nameA = bindingA.get(); // NPE while evaluating

        System.out.println("Bound value is " + (nameA != null ? nameA : "null") + ".");

        parentModel.setChildModel(new ChildModel());

        StringBinding bindingB = createBinding(parentModel);

        String nameB = bindingB.get();

        System.out.println("Bound value is " + (nameB != null ? nameB : "null") + ".");

    }

    private static StringBinding createBinding(ParentModel parentModel) {

        return Bindings.selectString(parentModel.childModelProperty(), "name");

    }

    public static class ParentModel {

        private final ObjectProperty<ChildModel> childModel = new SimpleObjectProperty<>();

        public final ReadOnlyObjectProperty<ChildModel> childModelProperty() {return childModel;}

        public final ChildModel getChildModel() {return childModel.get();}

        public final void setChildModel(ChildModel childModel) {this.childModel.set(childModel);}

    }

    public static class ChildModel {

        private final StringProperty name = new SimpleStringProperty("Child Model Name");

        public final ReadOnlyStringProperty nameProperty() {return name;}

        public final String getName() {return name.get();}

        protected final void setName(String name) {this.name.set(name);}

    }

}

..causes a NPE while evaluating the binding:

WARNING: Exception while evaluating select-binding [name]

Jan 24, 2014 7:10:57 AM com.sun.javafx.binding.SelectBinding$SelectBindingHelper getObservableValue

INFO: Property 'name' in ReadOnlyObjectProperty [bean: SelectBindingExample$ParentModel@4437c4, name: childModel, value: null] is null

java.lang.NullPointerException

  at com.sun.javafx.binding.SelectBinding$SelectBindingHelper.getObservableValue(SelectBinding.java:481)

  at com.sun.javafx.binding.SelectBinding$AsString.computeValue(SelectBinding.java:394)

  at javafx.beans.binding.StringBinding.get(StringBinding.java:152)

  at SelectBindingExample.main(SelectBindingExample.java:9)

  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

  at java.lang.reflect.Method.invoke(Method.java:483)

  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

What's going wrong here?  I've updated the example to work with the Bindings#selectString method from Java 7 and things work as expected.  Is this a bug?

Slight modification to the example so it works with Java 7.

This post has been answered by James_D on Jan 24 2014
Jump to Answer

Comments

AlbertoFaenza
Hi,

welcome to the forum.

Please read 2176552

How do you pretend to get an answer with such questions?
Where is your table structure?
Where is your sample data?

Regards.
Al
L-MachineGun
Also, be aware we DO NOT do the homework for you but rather may help you if you get "stuck".
You need to work it out yourself and post the results.
:p
sb92075
Handle: 945059
Status Level: Newbie
Registered: Jul 8, 2012
Total Posts: 213
Total Questions: 42 (34 unresolved)

why do you waste time here when most of your questions remain unanswered?
APC
The one thing more annoying than people who post code without using the {noformat}
{noformat} tag to make it readable and those people who use {noformat}
{noformat} tags to render ordinary text unreadable.

You have posted more than two hundred times. That really ought to be sufficient both to understand how to use the forum software properly and to grasp the etiquette of asking your questions in an appropriate way.

So please edit your question and fix the formatting.

Your future co-operation is appreciated.

Cheers, APC
stefan nebesnak
Could you post the schema of these tables?
SQL*Plus command:
 
SQL> DESC TIS_EVENT
SQL> DESC TIS_DT
SQL> DESC TIS_EVENT_DT_RLTSHP
Your requirement is very unclear, it's difficult to suggest the correct way, check the following example:
SELECT t.r_id_event4, 
       t.event_id, 
       t2.event_desc, 
       To_char(t3.DATE, 'YYYY') AS Year 
FROM   tis_event_dt_rltshp t --EVENT DATE RELATIONSHIP 
       join tis_event t2 USING (event_id) --EVENT 
       join tis_dt t3 USING (event_id) --DATE 
WHERE  t2.annual_event_flg = 'Y' 
       AND To_char(t3.DATE, 'YYYY') = '2013'; 
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 6 2014
Added on Jan 24 2014
7 comments
6,113 views