{"record":{"id":"33de2d517dbc991d","repo":"PhilJay/MPAndroidChart","slug":"bardata-needs-to-hold-at-least-2-bardatasets-to-al","errorCode":null,"errorMessage":"BarData needs to hold at least 2 BarDataSets to allow grouping.","messagePattern":"BarData needs to hold at least 2 BarDataSets to allow grouping\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.java","lineNumber":60,"sourceCode":"    public float getBarWidth() {\n        return mBarWidth;\n    }\n\n    /**\n     * Groups all BarDataSet objects this data object holds together by modifying the x-value of their entries.\n     * Previously set x-values of entries will be overwritten. Leaves space between bars and groups as specified\n     * by the parameters.\n     * Do not forget to call notifyDataSetChanged() on your BarChart object after calling this method.\n     *\n     * @param fromX      the starting point on the x-axis where the grouping should begin\n     * @param groupSpace the space between groups of bars in values (not pixels) e.g. 0.8f for bar width 1f\n     * @param barSpace   the space between individual bars in values (not pixels) e.g. 0.1f for bar width 1f\n     */\n    public void groupBars(float fromX, float groupSpace, float barSpace) {\n\n        int setCount = mDataSets.size();\n        if (setCount <= 1) {\n            throw new RuntimeException(\"BarData needs to hold at least 2 BarDataSets to allow grouping.\");\n        }\n\n        IBarDataSet max = getMaxEntryCountSet();\n        int maxEntryCount = max.getEntryCount();\n\n        float groupSpaceWidthHalf = groupSpace / 2f;\n        float barSpaceHalf = barSpace / 2f;\n        float barWidthHalf = mBarWidth / 2f;\n\n        float interval = getGroupWidth(groupSpace, barSpace);\n\n        for (int i = 0; i < maxEntryCount; i++) {\n\n            float start = fromX;\n            fromX += groupSpaceWidthHalf;\n\n            for (IBarDataSet set : mDataSets) {\n","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/PhilJay/MPAndroidChart/blob/9c7275a0596a7ac0e50ca566e680f7f9d73607af/MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.java#L42-L78","documentation":"Thrown by BarData.groupBars() when fewer than two BarDataSets are present. Grouping arranges multiple datasets side by side per x-value and inserts group/bar spacing, so a single dataset has nothing to group. The check is setCount <= 1, which rejects both zero and one dataset. Remember to call notifyDataSetChanged() on the chart after a successful grouping.","triggerScenarios":"Calling barData.groupBars(...) after adding only one BarDataSet. Calling groupBars() before all datasets are added. Removing datasets (e.g. user toggling series off) and leaving the groupBars() call in place.","commonSituations":"Building a grouped bar chart but only one series is loaded from the backend; dynamically filtering datasets down to one and re-running layout code; copy-pasted grouping call from a multi-series example into a single-series chart.","solutions":["Ensure BarData holds >= 2 BarDataSets before calling groupBars(): new BarData(set1, set2) or data.addDataSet(set2).","Guard: if (barData.getDataSetCount() >= 2) barData.groupBars(...); else fall back to plain bar rendering.","Only call groupBars() when the user has selected/loaded at least two series.","After grouping, call chart.notifyDataSetChanged() and chart.invalidate()."],"exampleFix":"// before\nBarData data = new BarData(set1); // only one set\ndata.groupBars(0f, 0.8f, 0.1f); // throws\n\n// after\nBarData data = new BarData(set1, set2);\ndata.setBarWidth(0.3f);\ndata.groupBars(0f, 0.8f, 0.1f);\nchart.notifyDataSetChanged();","handlingStrategy":"validation","validationCode":"if (barData.getDataSetCount() >= 2) {\n    barData.groupBars(fromX, groupSpace, barSpace);\n    chart.notifyDataSetChanged();\n}","typeGuard":"boolean canGroup(BarData data) {\n    return data != null && data.getDataSetCount() >= 2;\n}","tryCatchPattern":null,"preventionTips":["Only call groupBars() after confirming >= 2 BarDataSets are added.","When datasets are dynamically filtered, re-check the count before re-grouping.","Keep grouping and notifyDataSetChanged() together so state stays consistent."],"tags":["bar-data","grouping","dataset-count","validation"],"backgroundTag":null,"analyzedSha":"9c7275a0596a7ac0e50ca566e680f7f9d73607af","analyzedAt":"2026-08-14T00:17:56.136Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}