PhilJay/MPAndroidChart · error · IllegalArgumentException
entries array is NULL
Error message
entries array is NULL
What it means
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.
Source
Thrown at MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java:164
* default constructor
*/
public Legend() {
this.mTextSize = Utils.convertDpToPixel(10f);
this.mXOffset = Utils.convertDpToPixel(5f);
this.mYOffset = Utils.convertDpToPixel(3f); // 2
}
/**
* Constructor. Provide entries for the legend.
*
* @param entries
*/
public Legend(LegendEntry[] entries) {
this();
if (entries == null) {
throw new IllegalArgumentException("entries array is NULL");
}
this.mEntries = entries;
}
/**
* This method sets the automatically computed colors for the legend. Use setCustom(...) to set custom colors.
*
* @param entries
*/
public void setEntries(List<LegendEntry> entries) {
mEntries = entries.toArray(new LegendEntry[entries.size()]);
}
public LegendEntry[] getEntries() {
return mEntries;
}
View on GitHub (pinned to 9c7275a059)
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.
Example fix
// before Legend legend = new Legend(null); // throws // after LegendEntry[] entries = new LegendEntry[0]; // or real entries Legend legend = new Legend(entries); // or use the existing chart legend: chart.getLegend().setCustom(entries);
Defensive patterns
Strategy: validation
Validate before calling
if (entries != null) {
Legend legend = new Legend(entries);
} else {
throw new IllegalStateException("Legend entries must not be null");
} Type guard
boolean isValidEntries(LegendEntry[] e) {
return e != null;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Object Pool must be instantiated with a capacity greater tha
- BarData needs to hold at least 2 BarDataSets to allow groupi
AI-assisted analysis of PhilJay/MPAndroidChart@9c7275a059 (2026-08-14).
Data as JSON: /api/errors/cbd181d48bfa925c.
Report an issue: GitHub.