halfhp / androidplot

Charts and plots for Android

Home Page:http://androidplot.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Should check if canvas is null before drawing the chart

mslavkovski opened this issue · comments

The canvas parameter of the onDraw method can be null therefore it should be checked before attempting to draw the chart. In the case below, the app is crashing when the background rendering task that is printing charts in PDF is canceled before views are fully rendered and the cause of that crash is traced to the onDraw method.

06-12 12:39:43.814 18988-19253/my.domain.myapp E/com.androidplot.Plot: Exception while rendering Plot.
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Canvas.drawColor(int, android.graphics.PorterDuff$Mode)' on a null object reference
at com.androidplot.Plot.renderOnCanvas(Plot.java:817)
at com.androidplot.Plot$1.run(Plot.java:390)
at java.lang.Thread.run(Thread.java:818)
06-12 12:39:43.817 18988-19252/my.domain.myapp E/com.androidplot.Plot: Exception while rendering Plot.
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Canvas.drawColor(int, android.graphics.PorterDuff$Mode)' on a null object reference
at com.androidplot.Plot.renderOnCanvas(Plot.java:817)
at com.androidplot.Plot$1.run(Plot.java:390)
at java.lang.Thread.run(Thread.java:818)
06-12 12:39:43.836 18988-19254/my.domain.myapp E/com.androidplot.Plot: Exception while rendering Plot.
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Canvas.drawColor(int, android.graphics.PorterDuff$Mode)' on a null object reference
at com.androidplot.Plot.renderOnCanvas(Plot.java:817)
at com.androidplot.Plot$1.run(Plot.java:390)
at java.lang.Thread.run(Thread.java:818)
'

Thanks for the bug report - are there any steps to reproduce?

At the moment I can't create isolated project in which you can reproduce the problem but you can easily fix this by checking if the Canvas is null before calling the render function.

Of Course, I will try to find some time to create a sample project in which I can reproduce this. Currently everything I have can't be public.

Thanks! As far as adding the null check goes - I'd like to understand the conditions causing the problem first before fixing. it's possible that adding the check here will just push the bug further down into the code where it will be more difficult to diagnose in the future.