PhilJay/MPAndroidChart · error · RuntimeException

PieChart has no XAxis

Error message

PieChart has no XAxis

What it means

PieChart.getXAxis() always throws a RuntimeException because a pie chart has no XAxis concept (slices are arranged by angle, not by an x-axis). The method is kept only for API compatibility with the Chart base class and is annotated @Deprecated to signal it should never be used. Calling it directly, or via reflection/generic code that treats every chart through the base API, triggers the crash.

Source

Thrown at MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java:341

     * calculates the needed angle for a given value
     *
     * @param value
     * @param yValueSum
     * @return
     */
    private float calcAngle(float value, float yValueSum) {
        return value / yValueSum * mMaxAngle;
    }

    /**
     * This will throw an exception, PieChart has no XAxis object.
     *
     * @return
     */
    @Deprecated
    @Override
    public XAxis getXAxis() {
        throw new RuntimeException("PieChart has no XAxis");
    }

    @Override
    public int getIndexForAngle(float angle) {

        // take the current angle of the chart into consideration
        float a = Utils.getNormalizedAngle(angle - getRotationAngle());

        for (int i = 0; i < mAbsoluteAngles.length; i++) {
            if (mAbsoluteAngles[i] > a)
                return i;
        }

        return -1; // return -1 if no index found
    }

    /**
     * Returns the index of the DataSet this x-index belongs to.

View on GitHub (pinned to 9c7275a059)

Solutions

  1. Remove all getXAxis() calls for PieChart instances; pie charts do not support an x-axis.
  2. Type-narrow before configuring axes: if (!(chart instanceof PieChart)) chart.getXAxis()... .
  3. Configure pie-specific styling via pieChart.getXAxis() replacement APIs (e.g. setEntryLabelColor, setDrawEntryLabels, setUsePercentValues).
  4. Search the codebase for getXAxis() and confirm none are reached by PieChart instances.

Example fix

// before
chart.getXAxis().setEnabled(false); // crashes if chart is a PieChart

// after
if (!(chart instanceof PieChart)) {
    chart.getXAxis().setEnabled(false);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(chart instanceof PieChart)) {
    chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
}

Type guard

boolean hasXAxis(Chart<?, ?> chart) {
    return !(chart instanceof PieChart);
}

Prevention

When it happens

Trigger: Calling pieChart.getXAxis() directly to set labels/position. Generic helper code that loops over Chart instances and calls getXAxis().setEnabled(...). Migration of a BarChart/LineChart to PieChart without removing XAxis configuration calls. Using a third-party theming library that touches XAxis on all charts.

Common situations: Converting an existing bar/line chart screen to a pie chart and leaving axis styling code; copy-pasting chart-setup code across chart types; static-analysis or theming code that assumes all Chart subclasses expose an XAxis.


AI-assisted analysis of PhilJay/MPAndroidChart@9c7275a059 (2026-08-14). Data as JSON: /api/errors/de798415980700ae. Report an issue: GitHub.