SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and iOS Chart & Android Chart Components

Answered
0
0

I tried to create a simple pie chart, but my app crashes with the following expection:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.scichart.drawing.common.FontStyle.initPaint(android.graphics.Paint)' on a null object reference
    at com.scichart.charting.visuals.renderableSeries.PieDonutDrawingManagerBase.a(SourceFile:118)
    at com.scichart.charting.visuals.renderableSeries.PieDonutDrawingManagerBase.drawLabels(SourceFile:110)
    at com.scichart.charting.visuals.renderableSeries.PieRenderableSeries.internalDraw(SourceFile:70)
    at com.scichart.charting.visuals.renderableSeries.PieDonutRenderableSeriesBase.onDraw(SourceFile:560)
    at com.scichart.charting.visuals.c.onDraw(SourceFile:62)
    at android.view.View.draw(View.java:16178)

Here is my initialization code (Kotlin):

override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{
    super.onViewCreated(view, savedInstanceState)

    val chartSurface = view.findViewById(R.id.chart)
    val builder = SciChartBuilder.instance()

    val pieSeries = builder
        .newPieSeries()
        .withSegments(builder
                          .newPieSegment()
                          .withValue(40.0)
                          .build(),
                      builder
                          .newPieSegment()
                          .withValue(20.0)
                          .build())
        .build()

    chartSurface.renderableSeries.add(pieSeries)
}

The SciPieChartSurface is inside a CardView inside a Constraintlayout if that could matter:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">   

    <android.support.v7.widget.CardView
        android:id="@+id/card_chart"
       android:layout_width="match_parent"
       android:layout_height="match_parent" >

        <com.scichart.charting.visuals.SciPieChartSurface
            android:id="@+id/chart"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
Version
2.2.0.2348
  • You must to post comments
Best Answer
0
0

Hi Miha,

It’s a bit strange because I tried to run example with code which is similar to what you’ve provided and it worked as it should.

It looks like at least one of pie segments doesn’t have font style which is strange because by default it should pick up it from chart theme ( do you use some custom themes or something like this? ). Anyway I believe you can fix it by setting title style manually for each pie segment.

Unfortunately we don’t have a method for setting title style in builder so you need to extract and save each PieSegment and then call setTitleStyle() with desired font style. Also i believe you need to set some colors for your pie segments:

    final FontStyle pieChartFontStyle = sciChartBuilder.newFont().withTextColor(Color.WHITE).withTextSize(12, TypedValue.COMPLEX_UNIT_SP).build();

    final PieSegment pieSegment = sciChartBuilder.newPieSegment().withValue(40).build();
    pieSegment.setTitleStyle(pieChartFontStyle);
    pieSegment.setFillStyle(new SolidBrushStyle(Color.RED));

Does fix the exception?

Best regards,
Yura

  • MIha Rozina
    Thank you for your answer! You are correct, I have made a custom theme which I use for other charts and I have forgot to set the theme of this pie chart. Though I would think the default theme would be used if none was set? By the way, your code fix also fixed the problem :)
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.

SciChart WPF v6 BETA released! Note pricing/licensing changes are coming. Contact us to beat the rise!Read more
+