Skip to content

Updating a BoxChart by setting the BoxSeries data to an empty list causes an IndexOutOfBoundsException to be thrown. #891

@hogan-deng

Description

@hogan-deng

Describe the bug
If a BoxChart is updated by calling replaceData with an empty list on a BoxSeries obtained from chart.addSeries, the internal sanityCheck is bypassed, resulting in an IndexOutOfBoundsException.

To Reproduce
The following chart code can reproduce the issue:

public class OutOfBounds {
  public static void main(String[] args) throws ParseException {
    BoxChart chart = getBoxChart();
    new SwingWrapper(chart).displayChart();
  }

  public static BoxChart getBoxChart() {
    BoxChart chart = new BoxChartBuilder()
      .title("box plot demo")
      .xAxisTitle("X")
      .yAxisTitle("Y")
      .build();

    BoxSeries boxSeries = chart.addSeries("test", Arrays.asList(1, 2));
    boxSeries.replaceData(Arrays.asList());

    return chart;
  }
}

Screenshots

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.rangeCheck(ArrayList.java:659)
        at java.util.ArrayList.get(ArrayList.java:435)
        at org.knowm.xchart.internal.chartpart.BoxPlotDataCalculator.getQuartile(BoxPlotDataCalculator.java:122)
        at org.knowm.xchart.internal.chartpart.BoxPlotDataCalculator.calculate(BoxPlotDataCalculator.java:82)
        at org.knowm.xchart.internal.chartpart.BoxPlotDataCalculator.calculate(BoxPlotDataCalculator.java:48)
        at org.knowm.xchart.internal.chartpart.PlotContent_Box.doPaint(PlotContent_Box.java:50)
        at org.knowm.xchart.internal.chartpart.PlotContent_.paint(PlotContent_.java:61)
        at org.knowm.xchart.internal.chartpart.Plot_.paint(Plot_.java:37)
        at org.knowm.xchart.internal.chartpart.Plot_AxesChart.paint(Plot_AxesChart.java:34)
        at org.knowm.xchart.BoxChart.paint(BoxChart.java:156)

Expected behavior
The program should either run normally or issue a proper warning.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions