Search code examples
androidjsonnullpointerexceptionandroid-animationinputstream

Attempt to get length of null array when deserializing inputstream for Facebook Keyframes animation


I'm trying to use the Facebook Keyframes animations framework. I tried their sample and it is working fine, the problem is when I try with another JSON for another animation, I get a null pointer when deserializing the input stream. I'll attach my code and the errors

    InputStream stream = getResources().openRawResource(R.raw.animation);
    KFImage kfImage = null;
    try {
        kfImage = KFImageDeserializer.deserialize(stream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    KeyframesDrawable kfDrawable = new KeyframesDrawableBuilder().withImage(kfImage).build();
    
    mAnimation.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    mAnimation.setImageDrawable(kfDrawable);
    mAnimation.setImageAlpha(0);
    kfDrawable.startAnimation();

Error stack trace:

    java.lang.NullPointerException: Attempt to get length of null array
            at com.facebook.keyframes.util.ArgCheckUtil.checkTimingCurveObjectValidity(ArgCheckUtil.java:53)
            at com.facebook.keyframes.model.KFFeature.<init>(KFFeature.java:214)
            at com.facebook.keyframes.model.KFFeature$Builder.build(KFFeature.java:170)
            at com.facebook.keyframes.deserializers.KFFeatureDeserializer.readObject(KFFeatureDeserializer.java:90)
            at com.facebook.keyframes.deserializers.KFFeatureDeserializer$1.readObjectImpl(KFFeatureDeserializer.java:32)
            at com.facebook.keyframes.deserializers.KFFeatureDeserializer$1.readObjectImpl(KFFeatureDeserializer.java:29)
            at com.facebook.keyframes.deserializers.AbstractListDeserializer.readList(AbstractListDeserializer.java:35)
            at com.facebook.keyframes.deserializers.KFImageDeserializer.readObject(KFImageDeserializer.java:57)
            at com.facebook.keyframes.deserializers.KFImageDeserializer.deserialize(KFImageDeserializer.java:40)
            at MyFragment.onCreateView(MyFragment.java:83)
            at android.app.Fragment.performCreateView(Fragment.java:2053)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
            at android.app.BackStackRecord.run(BackStackRecord.java:834)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5597)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:984)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

Solution

  • Each entry in "feature_animations" array inside of loaded animation must contain "timing_curves" property. Its size must be equal to the number of keyframes - 1. Keyframes values are stored in "key_values" property that usually resides right above "timing_curves".

    This field must be always present. It can be empty but not null.

    Timing Curves describe the pace with which a transform changes between each keyframe. Each timing curve is modeled as a cubic bezier curve from point [0, 0] to [1, 1], where the X value is the progression in time from the origin keyframe to the destination keyframe, and the Y value describes the amount of change in the value at a given time: origValue + (destValue - origValue) * Y.

    Source of quote

    ArgCheckUtil verifies next condition that has failed for your animation file:

    /**
       * Checks that the format of a timing curve 3D float array is valid.  The number of timing curves
       * for an animation should be equal to the number of keyframes - 1.
       * @param timingCurves the 3D float array to check
       * @param keyFrameQuantity the number of key frames this animation has
       * @return true if the format is valid, false otherwise
       */
      public static boolean checkTimingCurveObjectValidity(...
    

    Source code link on GitHub

    You can also analyze sample animation file in android-sample project in