question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullPointerException getLegendRenderType(XYSeries.java:44)

See original GitHub issue

Hello,

I’m using version 3.5.4 and get an exception in the AWT-EventQueue thread. The exception comes quite arbitrary and not every Time I’m running the process.

As shown in the readme I’m quite certain that I’m using the api correctly. Compared to the advanced Example the only difference I can see is that I’m not creating my JFrame in the invokeLater method.

Even so, the chart looks completely fine and everything works without a problem. The chart in question if that matters: java_2019-07-29_17-21-05

The full Stacktrace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at org.knowm.xchart.XYSeries.getLegendRenderType(XYSeries.java:44)
	at org.knowm.xchart.internal.chartpart.Legend_Marker.getSeriesLegendRenderGraphicHeight(Legend_Marker.java:165)
	at org.knowm.xchart.internal.chartpart.Legend_Marker.getSeriesLegendRenderGraphicHeight(Legend_Marker.java:15)
	at org.knowm.xchart.internal.chartpart.Legend_.getBoundsHintVertical(Legend_.java:196)
	at org.knowm.xchart.internal.chartpart.Legend_.getBounds(Legend_.java:378)
	at org.knowm.xchart.internal.chartpart.AxisPair.paint(AxisPair.java:112)
	at org.knowm.xchart.XYChart.paint(XYChart.java:415)
	at org.knowm.xchart.XChartPanel.paintComponent(XChartPanel.java:90)
	at javax.swing.JComponent.paint(JComponent.java:1056)
	at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
	at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
	at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
	at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
	at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
	at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
	at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
	at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

For completeness how I’m using xchart. I’m creating a chart like this:

XYChart chart = new XYChartBuilder()
                .width(1000)
                .height(500)
                .title(label)
                .xAxisTitle("Generation")
                .yAxisTitle("Fitness")
                .theme(Styler.ChartTheme.GGPlot2)
                .build();

chart.getStyler().setDefaultSeriesRenderStyle(XYSeries.XYSeriesRenderStyle.Line);
chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideE);
chart.getStyler().setMarkerSize(5);

JPanel chartPanel = new XChartPanel<XYChart>(chart);

Don’t know if it’s important but I take the chart, and chartPanel object and store them in a different object for access when I need them.

I’m adding the chart onto a JTabbedPane with the initial draw like this

javax.swing.SwingUtilities.invokeLater(() -> {
             frame.validate();
             frame.pack();
             frame.setVisible(true);
});

Everey time i use the updateXYSeries method i repaint the respective chartPanel.

javax.swing.SwingUtilities.invokeLater(() -> {
        chartPanel.revalidate();
        chartPanel.repaint();
});

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Mr14huashaocommented, Feb 26, 2020

@Blaxzter Your sample code does not call xySeries.getLegendRenderType (). I carefully looked at the error stack information and code, and your description.

In the example you gave, the initial XYChart did not add a series, and then added a series. This situation may occur when the same XYChart is drawn by multiple threads. When the first thread draws XYChart, the series is still empty, and XYSeries.xySeriesRenderStyle may still be null

for (XYSeries xySeries : getSeriesMap().values()) {
  XYSeries.XYSeriesRenderStyle chartXYSeriesRenderStyle =
	  xySeries.getXYSeriesRenderStyle(); // would be directly set
  if (chartXYSeriesRenderStyle == null) { // wasn't overridden, use default from Style Manager
	xySeries.setXYSeriesRenderStyle(getStyler().getDefaultSeriesRenderStyle());
  }
}

At the same time, the second thread added the series first when drawing XYChart, and the first thread continued to draw. drawing AxisPair will use XYSeries.xySeriesRenderStyle, which will cause NullPointerException.

Like you said it is really easy to solve, add code:

xySeries.setXYSeriesRenderStyle(myXYChart.getDefaultSeriesRenderStyle());

If you think this is not the case, this problem will definitely occur. You can paste the sample code and I can continue to analyze.

0reactions
Blaxztercommented, Feb 25, 2020

@Mr14huashao In which line of my own code do I call xySeries.getLegendRenderType() ?

And if you’d be able to look at the full stack trace you’d see that it’s an internal call made while painting. And as I originally stated: I just copied the given advanced example.

So I think this issue is either not good explained on how to fix it properly or it’s just not resolved correctly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found