0

I pass the details/reasons and I go directly to the problem: I have a JFreeChart (Display class) and a collection of points (CollectionPoint class). When "collectionPoint", not as a Thread, (which creates a few series of points) accesses to "display", everything is fine. When collectionPoint, as a Thread, accesses to "display", I get a bunch of errors. I do not understand. What's going on?

Display class:

public class Display extends JFrame {

    private JFreeChart chart;
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    ...
    ...
    // Here the method called
    public synchronized void addSeries(TimeSeries series) {
        dataset.addSeries(series);
        System.out.println("series added");
    }
    ...
}

CollectionPoint (NOT as Thread)

//public class CollectionPoint extends Thread {
public class CollectionPoint {

    private int i;
    private TimeSeries series;
    private ArrayList<TimeSeries> seriesArray;
    private Display display;

    public CollectionPoint() {
        run1();
    }

    public void run1() {

        display = new Display();
        seriesArray = new ArrayList<TimeSeries>();

        for (int i = 0; i < 10; i++) {
            series = new TimeSeries("" + i);
            makeSeries();
            seriesArray.add(series);
        }

        System.out.println("okay");

        seriesArray.forEach((series) -> {
            displayChart(series);
        });
    }

    private void displayChart(TimeSeries series) {
        display.addSeries(series);
    }

    public void makeSeries() {
        for (int j = 0; j < 80000; j++) {
            double _timestamp = (double) j;
            Timestamp timestamp = new Timestamp((long) ((_timestamp * 1000)));
            series.add(new Millisecond(timestamp), j * 2 * i);
        }
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(() -> {
            var ex = new CollectionPoint();
            //ex.start();
        });
    }
}

CollectionPoint (as Thread)

public class CollectionPoint extends Thread {
//public class CollectionPoint {

    private int i;
    private TimeSeries series;
    private ArrayList<TimeSeries> seriesArray;

    private Display display;

    public CollectionPoint() {
        //run1();
    }

    public void run() {

        display = new Display();
        seriesArray = new ArrayList<TimeSeries>();

        for (int i = 0; i < 10; i++) {
            series = new TimeSeries("");
            makeSeries();
            seriesArray.add(series);
        }

        System.out.println("okay");

        seriesArray.forEach((series) -> {
            displayChart(series);
        });
    }

    private void displayChart(TimeSeries series) {
        display.addSeries(series);
    }

    public void makeSeries() {
        for (int j = 0; j < 80000; j++) {
            double _timestamp = (double) j;
            Timestamp timestamp = new Timestamp((long) ((_timestamp * 1000)));
            series.add(new Millisecond(timestamp), j * 2 * i);
        }
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(() -> {
            var ex = new CollectionPoint();
            ex.start();
        });
    }

}

Errors...

okay
series added
series added
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class sun.util.calendar.Gregorian$Date cannot be cast to class sun.util.calendar.JulianCalendar$Date (sun.util.calendar.Gregorian$Date and sun.util.calendar.JulianCalendar$Date are in module java.base of loader 'bootstrap')
    at java.base/sun.util.calendar.JulianCalendar.getCalendarDateFromFixedDate(JulianCalendar.java:186)
    at java.base/java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2410)
    at java.base/java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2815)
    at java.base/java.util.Calendar.updateTime(Calendar.java:3431)
    at java.base/java.util.Calendar.getTimeInMillis(Calendar.java:1813)
    at org.jfree.data.time.Millisecond.getFirstMillisecond(Millisecond.java:429)
    at org.jfree.data.time.TimeSeriesCollection.getX(TimeSeriesCollection.java:390)
    at org.jfree.data.time.TimeSeriesCollection.getXValue(TimeSeriesCollection.java:362)
    at org.jfree.chart.renderer.xy.XYLineAndShapeRenderer.drawPrimaryLine(XYLineAndShapeRenderer.java:787)
    at org.jfree.chart.renderer.xy.XYLineAndShapeRenderer.drawItem(XYLineAndShapeRenderer.java:713)
    at org.jfree.chart.plot.XYPlot.render(XYPlot.java:3447)
    at org.jfree.chart.plot.XYPlot.draw(XYPlot.java:2980)
    at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1160)
    at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1452)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
    at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5255)
    at java.desktop/javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:246)
    at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
    at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5203)
    at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5013)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:865)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
    at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
    at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1884)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
series added
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index 13 out of bounds for length 13
    at java.base/sun.util.calendar.BaseCalendar.getCalendarDateFromFixedDate(BaseCalendar.java:453)
    at java.base/java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2394)
    at java.base/java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2815)
    at java.base/java.util.Calendar.updateTime(Calendar.java:3431)
    at java.base/java.util.Calendar.getTimeInMillis(Calendar.java:1813)
    at org.jfree.data.time.Millisecond.getFirstMillisecond(Millisecond.java:429)
    at org.jfree.data.time.TimeSeriesCollection.getX(TimeSeriesCollection.java:390)
    at org.jfree.data.time.TimeSeriesCollection.getXValue(TimeSeriesCollection.java:362)
    at org.jfree.chart.renderer.xy.XYLineAndShapeRenderer.drawPrimaryLine(XYLineAndShapeRenderer.java:787)
    at org.jfree.chart.renderer.xy.XYLineAndShapeRenderer.drawItem(XYLineAndShapeRenderer.java:713)
    at org.jfree.chart.plot.XYPlot.render(XYPlot.java:3447)
    at org.jfree.chart.plot.XYPlot.draw(XYPlot.java:2980)
    at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1160)
    at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1452)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
    at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5255)
    at java.desktop/javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:246)
    at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
    at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5203)
    at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5013)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:865)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
    at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
    at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1884)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
series added
series added
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException
Exception in thread "Thread-0" java.lang.ClassCastException

? :\

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Vids
  • 1
  • 1
  • 1
    Construct and manipulate Swing GUI objects _only_ on the [event dispatch thread](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). One alternative approach is shown [here](https://stackoverflow.com/a/13205322/230513). – trashgod May 29 '21 at 01:43
  • Thank you very much for the hint! I am working on it ;) – Vids May 29 '21 at 08:12
  • If you have continued problems, please [edit] your question to include a [mcve] that shows your revised approach. Going forward, you can [answer your own question](http://meta.stackoverflow.com/q/17463/163188), or I can close it as a duplicate. – trashgod May 30 '21 at 02:38
  • I am still working on it... Once solved, I will post my revised approach as you suggested. – Vids May 30 '21 at 07:42

0 Answers0