PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.53k stars 9.01k forks source link

Bug: PieChartRenderer - NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference #3470

Open Johnson145 opened 6 years ago

Johnson145 commented 6 years ago

mDrawBitmap seems to be null and thereby causing a NullPointerException. Here is the full stacktrace:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference at com.github.mikephil.charting.renderer.PieChartRenderer.drawExtras(PieChartRenderer.java:638) at com.github.mikephil.charting.charts.PieChart.onDraw(PieChart.java:131) at android.view.View.draw(View.java:16238) at android.view.View.updateDisplayListIfDirty(View.java:15235) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617) at android.view.View.updateDisplayListIfDirty(View.java:15195) at android.view.View.draw(View.java:16008) at android.view.ViewGroup.drawChild(ViewGroup.java:3653) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3443) at android.view.View.updateDisplayListIfDirty(View.java:15230) at android.view.View.draw(View.java:16008) at android.view.ViewGroup.drawChild(ViewGroup.java:3653) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3443) at android.view.View.updateDisplayListIfDirty(View.java:15230) at android.view.View.draw(View.java:16008) at android.view.ViewGroup.drawChild(ViewGroup.java:3653) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3443) at android.view.View.updateDisplayListIfDirty(View.java:15230) at android.view.View.draw(View.java:16008) at android.view.ViewGroup.drawChild(ViewGroup.java:3653) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3443) at android.view.View.updateDisplayListIfDirty(View.java:15230) at android.view.View.draw(View.java:16008) at android.view.ViewGroup.drawChild(ViewGroup.java:3653) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3443) at android.view.View.updateDisplayListIfDirty(View.java:15230) at android.view.View.draw(View.java:16008) at android.view.ViewGroup.drawChild(ViewGroup.java:3653) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3443) at android.view.View.draw(View.java:16241) at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2709) at android.view.View.updateDisplayListIfDirty(View.java:15235) at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281) at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287) at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322) at android.view.ViewRootImpl.draw(ViewRootImpl.java:2681) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2495) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2124) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1140) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6232) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5551) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

Unfortunately, I do not have further information about the potential cause of this error. However, I already received multiple occurences of this bug in Crashlytics.

Any help is appreciated very much!

laconichy commented 6 years ago

me too, What is your solution? @Johnson145

Johnson145 commented 6 years ago

Unfortunately, I don't have any solution yet. I was hoping to get some support here..

TonyNikolov commented 6 years ago

This happens to me too on orientation change.

Johnson145 commented 6 years ago

Anybody checked out #3207 ? I'm not sure whether it will fix this issue, too, but I'll give it a shot.

justindannguyen commented 6 years ago

Hi there, my PR will resolve this problem.

Johnson145 commented 6 years ago

Thanks for your help. However, I implemented a different solution in #3763 that covers more use cases and tries to fix the broken references.

poyarkov95 commented 5 years ago

Thanks for your help. However, I implemented a different solution in #3763 that covers more use cases and tries to fix the broken references.

Excuse me, could you please explain how do I apply these changes with PirChartRenderer to my project? I am installing this library via gradle and I can't change source code. What sould I do ?

almic commented 5 years ago

@poyarkov95 I don't think this hasn't been added yet. You have to manually download the repository from those pull requests into your android project to get the fixes. However I do hope to pull a fix soon into this main repository

liorOrcam commented 5 years ago

Hi @almic , Will the fix be pulled soon into the repository? Thanks.

joaocamiloulhoa commented 5 years ago

I'm using the library v3.1.0 and I have the same problem! Any solution?

image

bnoirant commented 5 years ago

I'm in the same case. I tried using the latest commit on the v3.1.0 but nothing changed.

Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference at mikephil.charting.renderer.PieChartRenderer.drawExtras(PieChartRenderer.java:669)

bnoirant commented 5 years ago

I don't have this error anymore, so if it can help someone else: It may be (I'm not sure) because I was trying to draw (at least change the parameters of) the pie chart before drawing the view.

joaocamiloulhoa commented 5 years ago

Hi, thanks for the reply. I still have this error! Strange because it is only occurring on 1 smarthphone, all others work well! I'll test your tip !!

mauriiciobarbosa commented 5 years ago

Hi Guys,

I'm still facing the same bug on version v3.1.0. Any future version on roadmap to fix this problem?

abdnezar commented 4 years ago

Hi Guys,

I'm Have This Error With SDK 22 or Less , But work Good on SDK 23 or High.

akshaychavan7 commented 4 years ago

Did anyone find the solution to this problem?

GaidamakUA commented 3 years ago

@AkshayChavan7 As a workaround you can create custom view that extends PieChart and use this renderer.

class HackPieChartRenderer(
    chart: PieChart,
    animator: ChartAnimator,
    viewPortHandler: ViewPortHandler
) : PieChartRenderer(chart, animator, viewPortHandler) {

    override fun drawExtras(c: Canvas?) {
        if (mDrawBitmap != null) {
            super.drawExtras(c)
        }
    }
}

This works, because in my case bug was caused by view being 0dp tall. It was necessary for animation. And in that case mDrawBitmap doesn't get initialised, which makes sense. But drawExtras(...) still getting called.

appreciated commented 3 years ago

Here the Java Version of @GaidamakUA's workaround. Create the class WorkaroundPieChartRenderer:

class WorkaroundPieChartRenderer extends PieChartRenderer {

    public WorkaroundPieChartRenderer(PieChart chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(chart, animator, viewPortHandler);
    }

    @Override
    public void drawExtras(Canvas c) {
        if (mDrawBitmap != null) {
            super.drawExtras(c);
        }
    }
}

Overwrite the Renderer as follows:

public class PieChartWithWorkaround extends PieChart {
    ...
    public PieChartWithWorkaround(Context context) {
        super(context);
        setRenderer(new WorkaroundPieChartRenderer(this,this.mAnimator,this.mViewPortHandler));
        ...
    }
}
millie-mouse commented 3 years ago

我在运行时也出现了此问题,我一直尝试去从MPChart内部找解决方法,但是后面慢慢调试才发现在外部也可以找到解决方法。经过测试发现,是因为我的UI布局变化导致了PieChart显示不完全,之后我再xml文件中修改了布局结构这个问题就解决了。

I also encountered this problem when I was running. I have been trying to find a solution from within MPChart, but after slowly debugging, I found that a solution can also be found externally. After testing, it was found that the PieChart display was incomplete because of my UI layout changes. After that, I modified the layout structure in the xml file to solve the problem.

dalegaspi commented 2 years ago

this happened to me if the layout_width and/or layout_height of the piechart in the activity/fragment xml is set to 0.

cakasuma commented 8 months ago

Any updates to this?, does the workaround included in the latest version?

mhmdeve commented 4 months ago

this happened to me if the layout_width and/or layout_height of the piechart in the activity/fragment xml is set to 0.

I agree with @dalegaspi. The actual issue is the layout height. Just make sure your chart layout is showing "No Chart data Available" when added to layout. If for any reason in your layout its not showing properly, it will crash. This will resolve the crash. That's common sense, if there is no enough height to render stuff, it will crash.