XChart icon indicating copy to clipboard operation
XChart copied to clipboard

NoSuchElementException after all data removed

Open sjaenick opened this issue 1 year ago • 2 comments

After a chart.getSeriesMap().clear();, NoSuchElementException is thrown when attempting to draw the component:

java.util.NoSuchElementException
	at java.base/java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:758)
	at java.base/java.util.LinkedHashMap$LinkedValueIterator.next(LinkedHashMap.java:783)
	at org.knowm.xchart.internal.chartpart.Axis.getAxisTickCalculator(Axis.java:442)
	at org.knowm.xchart.internal.chartpart.Axis.getXAxisHeightHint(Axis.java:287)
	at org.knowm.xchart.internal.chartpart.Axis.preparePaint(Axis.java:155)
	at org.knowm.xchart.internal.chartpart.AxisPair.paint(AxisPair.java:123)
	at org.knowm.xchart.CategoryChart.paint(CategoryChart.java:311)
	at org.knowm.xchart.XChartPanel.paintComponent(XChartPanel.java:168)
	at java.desktop/javax.swing.JComponent.paint(JComponent.java:1119)

Verified for xchart 3.8.3.

sjaenick avatar Mar 15 '23 13:03 sjaenick

I am able to reproduce the issue. Moreover, the same seems to happen when you don't add any series at all (which is a similar situation to adding series and removing them).

    Chart ch = new CategoryChart(800, 600, GGPlot2);
    ch.paint(new CustomGraphic(), 20, 20);

yields

java.util.NoSuchElementException
        at java.base/java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:766)
        at java.base/java.util.LinkedHashMap$LinkedValueIterator.next(LinkedHashMap.java:791)
        at org.knowm.xchart.internal.chartpart.Axis.getAxisTickCalculatorForX(Axis.java:515

I am wondering what is the cleanest behavior here. Likely some sanitization of input and explicit exception with "no series" or something.

mccartney avatar Jun 17 '23 19:06 mccartney

Not sure if there should be an exception; I'd prefer just displaying an empty chart.

sjaenick avatar Jun 18 '23 12:06 sjaenick