RamiJ3mli / PercentageChartView

An Android custom view that displays the progress of an ongoing task.
Apache License 2.0
377 stars 82 forks source link

ava.lang.NullPointerException: Attempt to invoke virtual method 'void com.ramijemli.percentagechartview.renderer.BaseModeRenderer.setProgress(float, boolean)' on a null object reference #27

Open yams585 opened 4 years ago

yams585 commented 4 years ago

Using a progressbar if I rotate the screen a few times or try to reload a few times eventually the library crashes with Process: com.phpni.member_android, PID: 6451 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.ramijemli.percentagechartview.renderer.BaseModeRenderer.setProgress(float, boolean)' on a null object reference at com.ramijemli.percentagechartview.PercentageChartView.setProgress(PercentageChartView.java:421)

vedprakashwagh commented 4 years ago

I have almost the same issue, except it crashes with the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.ramijemli.percentagechartview.renderer.BaseModeRenderer.destroy()' on a null object reference
        at com.ramijemli.percentagechartview.PercentageChartView.onDetachedFromWindow(PercentageChartView.java:175)
        at android.view.View.dispatchDetachedFromWindow(View.java:21362)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4641)
        at android.view.ViewGroup.removeViewInternal(ViewGroup.java:6287)
        at android.view.ViewGroup.removeViewInternal(ViewGroup.java:6258)
        at android.view.ViewGroup.removeView(ViewGroup.java:6189)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:973)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:434)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7860)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
BorisLegovic commented 4 years ago

Same problem here: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.ramijemli.percentagechartview.renderer.BaseModeRenderer.setProgress(float, boolean)' on a null object reference

I was not able to simulate error, but I got it in the crash report.

yams585 commented 4 years ago

@BorisLegovic all you have to do is turn your phone a few times and you'll see that npe thrown.

yams585 commented 4 years ago

I'm handling on resume so this shouldn't be thrown.

vedprakashwagh commented 4 years ago

I solved this error by importing library manually in the app, and then running the app. This is occurred because of

@Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(renderer!=null) {
            renderer.destroy();
        }
        renderer = null;

        if (onProgressChangeListener != null) {
            onProgressChangeListener = null;
        }
    }

When the View is detached from Window, renderer object is destroyed. And this happens when you lock the device or when the view is basically detached. I solved it by commenting the renderer part in onDetachedFromWindow() so it doesn't get destroyed. The thing is this might cause memory leak, but I haven't seen any issue in my app after seeing the profiler, so I guess you should be fine.

rkarimi88 commented 4 years ago

Hi. Your library is great, but I have the same problem. May I ask you to update the library please?

I also have problem in adding library to project manually. So I can't use the solution given by "vedprakashwagh".

Thanks in advance.

vladimir-petric commented 3 years ago

Yes, I am experiencing this same issue. And @vedprakashwagh is right about the cause of the problem, I had the same conclusion

anilejder commented 2 years ago

I faced this problem too. However, I have used it in bottom sheet dialog. So, I remove whole the view with binding.root.removeAllViews() in onDismiss(). Keep in mind.

VishalNehra commented 2 years ago

For me the stacktrace was in onSaveInstanceState

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'float com.ramijemli.percentagechartview.renderer.BaseModeRenderer.getStartAngle()' on a null object reference
        at com.ramijemli.percentagechartview.PercentageChartView.onSaveInstanceState(PercentageChartView.java:198)
        at android.view.View.dispatchSaveInstanceState(View.java:20797)
        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:3992)
        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:3992)
        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:3992)

My activity don't really need to save state, it's rotation is locked anyways, so I've used percentageChartView.isSaveEnabled = false

khizarhayat commented 1 year ago

I was facing same issue in stack trace it was clear that it is coming in onDestroyView so instead i download library code i extend PercentageChartView and override method

override fun onDetachedFromWindow() {
    try {
        super.onDetachedFromWindow()
    } catch (e: Exception) {
        e.printStackTrace()
    }
}
samantrader commented 1 year ago

I solved this error by importing library manually in the app, and then running the app. This is occurred because of

@Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(renderer!=null) {
            renderer.destroy();
        }
        renderer = null;

        if (onProgressChangeListener != null) {
            onProgressChangeListener = null;
        }
    }

When the View is detached from Window, renderer object is destroyed. And this happens when you lock the device or when the view is basically detached. I solved it by commenting the renderer part in onDetachedFromWindow() so it doesn't get destroyed. The thing is this might cause memory leak, but I haven't seen any issue in my app after seeing the profiler, so I guess you should be fine.

Hi
where is put this code exactly ?

thanks