MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

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

Open Johnson145 opened this issue 8 years ago • 22 comments

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!

Johnson145 avatar Sep 30 '17 12:09 Johnson145

me too, What is your solution? @Johnson145

laconichy avatar Oct 23 '17 05:10 laconichy

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

Johnson145 avatar Oct 23 '17 09:10 Johnson145

This happens to me too on orientation change.

TonyNikolov avatar Oct 29 '17 15:10 TonyNikolov

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

Johnson145 avatar Nov 15 '17 11:11 Johnson145

Hi there, my PR will resolve this problem.

justindannguyen avatar Nov 25 '17 00:11 justindannguyen

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

Johnson145 avatar Jan 29 '18 15:01 Johnson145

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 ?

poyarkov95 avatar Nov 21 '18 09:11 poyarkov95

@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

almic avatar Nov 24 '18 14:11 almic

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

liorOrcam avatar Feb 04 '19 12:02 liorOrcam

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

image

joaocamiloulhoa avatar Jun 25 '19 10:06 joaocamiloulhoa

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 avatar Jun 28 '19 09:06 bnoirant

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.

bnoirant avatar Jul 05 '19 10:07 bnoirant

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 !!

joaocamiloulhoa avatar Jul 05 '19 11:07 joaocamiloulhoa

Hi Guys,

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

mauriiciobarbosa avatar Sep 04 '19 16:09 mauriiciobarbosa

Hi Guys,

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

abdnezar avatar Jul 02 '20 22:07 abdnezar

Did anyone find the solution to this problem?

akshaychavan7 avatar Aug 24 '20 08:08 akshaychavan7

@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.

GaidamakUA avatar Sep 17 '20 09:09 GaidamakUA

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));
        ...
    }
}

appreciated avatar Jan 07 '21 14:01 appreciated

我在运行时也出现了此问题,我一直尝试去从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.

millie-mouse avatar Jun 03 '21 07:06 millie-mouse

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

dalegaspi avatar Apr 11 '22 22:04 dalegaspi