Android MP Chart highlightValue not working, throws ArrayIndexOutOfBoundsException

Ask A Question

Login To post a question

I’m playing around with Android MP Chart lib to draw awesome charts.

I’m trying to highlight values on my chart, but it does not puts the highlight to the right place, or throws ArrayIndexOutOfBoundsException.

I’ve made a little dummy project for it. When user clicks on the next button, the highlight should move to positive direction.

  public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    final int DATA_MAX_COUNT = 30;

    List<MyData> list    = new ArrayList<>(); ///<Dummy data stored in here
    List<Entry>  entries = new ArrayList<>(); ///<Entries for MP Chart

    int highlightIndex = 0; ///<Chart's data index to be highlighted

    CombinedChart combinedChart; ///<I use combined chart because there will be more data sets added later on

    Button prevBtn; ///<Button for highlight control
    Button nextBtn; ///<Button for highlight control

    protected void onCreate(Bundle savedInstanceState) {

        combinedChart = (CombinedChart) findViewById(;

        prevBtn = (Button) findViewById(;

        nextBtn = (Button) findViewById(;


    public void onClick(View v) {

        //Clicking buttons should move the highlighted value
        if (v.equals(prevBtn)) {
            if (highlightIndex > 0) {
        } else if (v.equals(nextBtn)) {
            if (highlightIndex + 1 < DATA_MAX_COUNT) {

        //Does not work, throws exception
        //combinedChart.highlightValue(new Highlight(highlightIndex, 0, 0));

        //Does not work, throws exception
        //combinedChart.highlightValue(highlightIndex, 0, false);


//        java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
//        at
//        at
//        at com.github.mikephil.charting.charts.Chart.highlightValue(
//        at com.github.mikephil.charting.charts.Chart.highlightValue(

        //Works, but highlights value on chart with like x=0 and y= 190, wtf?
        combinedChart.highlightValue(combinedChart.getHighlighter().getHighlight(highlightIndex, 0));


    //Generating random data to a list
    public void generateData() {
        for (int i = 0; i < DATA_MAX_COUNT; i++) {
            MyData myData = new MyData(new Random().nextInt(100) + 100);

    //Simple func for adding data to entries and drawing chart
    private void drawChart() {

        CombinedData combinedData = new CombinedData();

        for (int i = 0; i < list.size(); i++) {
            MyData myData = list.get(i);
            entries.add(new Entry(i, myData.getValue(), myData));

        LineDataSet lineDataSet = new LineDataSet(entries, "My data list");


        LineData lineData = new LineData();



    //Dummy data class
    public static class MyData {

        private int value;

        public MyData(int value) {
            this.value = value;

        public int getValue() {
            return value;

        public void setValue(int value) {
            this.value = value;

I don’t get it why I’m getting

java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1

Because what the hell is -1? and what is 10? There are not a single thing in my code which is 10, and why does any of the chart’s functions getting -1?

I’m using


Please help if you can.

E D I T:

I’ve added combinedChart.setOnChartValueSelectedListener(this); to the chart.

With this callback onValueSelected(Entry, Highlight) I can test this thing in another way. It gives a Highlight object. If I does nothing in this callback, the chart draws the highlights well. If I call it programatically with the same X value, it throws the usual exception or draws the highlight to wrong position. (to zero)

See the callback:

    public void onValueSelected(Entry e, Highlight h) {
        float x = h.getX();

        Log.i("Highlighted", "Actual highlight: " + x);

        //Getting the same exception as above
        //combinedChart.highlightValue(x, 0, false);

        //Does not works, draws to x=0 position with any given x
        combinedChart.highlightValue(combinedChart.getHighlighter().getHighlight(x, 0));

For more info please click here

Have a question or need a custom quote?