{"record":{"id":"cbd181d48bfa925c","repo":"PhilJay/MPAndroidChart","slug":"entries-array-is-null","errorCode":null,"errorMessage":"entries array is NULL","messagePattern":"entries array is NULL","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java","lineNumber":164,"sourceCode":"     * default constructor\n     */\n    public Legend() {\n\n        this.mTextSize = Utils.convertDpToPixel(10f);\n        this.mXOffset = Utils.convertDpToPixel(5f);\n        this.mYOffset = Utils.convertDpToPixel(3f); // 2\n    }\n\n    /**\n     * Constructor. Provide entries for the legend.\n     *\n     * @param entries\n     */\n    public Legend(LegendEntry[] entries) {\n        this();\n\n        if (entries == null) {\n            throw new IllegalArgumentException(\"entries array is NULL\");\n        }\n\n        this.mEntries = entries;\n    }\n\n    /**\n     * This method sets the automatically computed colors for the legend. Use setCustom(...) to set custom colors.\n     *\n     * @param entries\n     */\n    public void setEntries(List<LegendEntry> entries) {\n        mEntries = entries.toArray(new LegendEntry[entries.size()]);\n    }\n\n    public LegendEntry[] getEntries() {\n        return mEntries;\n    }\n","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/PhilJay/MPAndroidChart/blob/9c7275a0596a7ac0e50ca566e680f7f9d73607af/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java#L146-L182","documentation":"Thrown by the Legend(LegendEntry[]) constructor when a null array is passed. The constructor delegates to this() then validates entries == null and rejects it with IllegalArgumentException, because a legend cannot be built from a missing entry set. This is the standard precondition for explicitly constructing a legend with custom entries.","triggerScenarios":"Calling new Legend((LegendEntry[]) null). Passing a computed array that resolved to null (e.g. entries not yet initialized). Splitting legend setup so the array is built lazily but the constructor runs first.","commonSituations":"Building a custom legend before the data set list is populated; deserializing legend entries where the source list was empty/missing and toArray produced null; refactoring that passes a field not yet assigned.","solutions":["Pass a non-null LegendEntry[] (an empty array is acceptable; null is not).","Guard: if (entries != null) chart.getLegend().setCustom(entries); else use chart.getLegend().setEntries(Collections.emptyList()).","Initialize the array before constructing the Legend: LegendEntry[] entries = buildEntries(); chart.getLegend().setCustom(entries).","Prefer setEntries(List) or setCustom(...) which accept lists and handle emptiness more gracefully."],"exampleFix":"// before\nLegend legend = new Legend(null); // throws\n\n// after\nLegendEntry[] entries = new LegendEntry[0]; // or real entries\nLegend legend = new Legend(entries);\n// or use the existing chart legend:\nchart.getLegend().setCustom(entries);","handlingStrategy":"validation","validationCode":"if (entries != null) {\n    Legend legend = new Legend(entries);\n} else {\n    throw new IllegalStateException(\"Legend entries must not be null\");\n}","typeGuard":"boolean isValidEntries(LegendEntry[] e) {\n    return e != null;\n}","tryCatchPattern":null,"preventionTips":["Prefer setCustom(List) or setEntries(List) which handle empties more gracefully.","Initialize the entry array at declaration; never pass a possibly-null field.","Unit-test the legend construction path with empty vs null arrays."],"tags":["legend","null-argument","constructor","validation"],"backgroundTag":null,"analyzedSha":"9c7275a0596a7ac0e50ca566e680f7f9d73607af","analyzedAt":"2026-08-14T00:17:56.136Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}