1. #11
    Sencha - GXT Dev Team
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,731
    Vote Rating
    90
    Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light

      0  

    Default


    Isn't that what should happen? What could it do instead other than break if the intermediate model is null?

    I just replied to another post where you asked a related question - we could continue this conversation there if you like. I tried to give a full example of a) why this must happen, and b) how to build your own ValueProvider with custom behavior.

  2. #12
    Sencha User
    Join Date
    Aug 2012
    Posts
    42
    Vote Rating
    0
    ramgovind is on a distinguished road

      0  

    Default


    It s working fine. Thank you so much!

  3. #13
    Sencha User
    Join Date
    Jan 2013
    Posts
    8
    Vote Rating
    0
    ryanshillington is on a distinguished road

      0  

    Default My solution

    My solution


    Hey folks,

    Here's a possible solution that I whipped up to solve NullPointerExceptions coming out of otherwise perfectly good ValueProviders.

    Code:
    import com.google.gwt.core.client.JavaScriptException;
    import com.sencha.gxt.core.client.ValueProvider;
    
    
    /**
     * The purpose of this class is to wrap a value provider but handle NullPointerExceptions gracefully.
     *
     * Note: JavsScript doesn't throw java.lang.NullPointerExceptions (that's a Java thing) so this method technicallly catches both NullPointerException
     * (for hosted mode), and JavaScriptExceptions (for production mode).
     */
    public class HandleNullValueProvider<T, V> implements ValueProvider<T, V> {
        private ValueProvider<T,V> innerValueProvider;
        private V defaultValue;
    
    
        /**
         * @param innerValueProvider The provider to delegate calls to.
         * @param defaultValue The value to provide if innerValueProvider should throw an exception
         */
        public HandleNullValueProvider(ValueProvider<T, V> innerValueProvider, V defaultValue) {
            this.innerValueProvider = innerValueProvider;
            this.defaultValue = defaultValue;
        }
    
    
        @Override
        public V getValue(T object) {
            try {
                return innerValueProvider.getValue(object);
            } catch (NullPointerException e) {
                return defaultValue;
            } catch (JavaScriptException e) {
                return defaultValue;
            }
        }
    
    
        @Override
        public void setValue(T object, V value) {
            try {
                innerValueProvider.setValue(object, value);
            } catch (NullPointerException ignored) {
            } catch (JavaScriptException ignored) {
            }
        }
    
    
        @Override
        public String getPath() {
            return innerValueProvider.getPath();
        }
    }
    Wherever you would normally put in a ValueProvider, you replace it with this:

    Code:
    new HandleNullValueProvider<Person, Long>(properties.companyId(), 0L)
    If Person.getCompanyId() ever return null, this ValueProvider will return the default, which is 0.

    P.S. I think it would be great if Sencha GXT included a class like this (or another mechanism to solve null values). It's painful to write custom ValueProviders for every value that *might* be null some day.

  4. #14
    Sencha Premium Member
    Join Date
    Apr 2008
    Posts
    27
    Vote Rating
    0
    smartin is on a distinguished road

      0  

    Default


    Nice solution!

    I also think Sencha should include a way to handle the Null values coming from DB via Beans

  5. #15
    Sencha User
    Join Date
    Nov 2010
    Posts
    2
    Vote Rating
    0
    himanshunp is on a distinguished road

      0  

    Default Check out this related thread.