0

The error is shown as,

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at org.knowm.xchart.internal.chartpart.Legend_AxesChart.paint(Legend_AxesChart.java:195)
    at org.knowm.xchart.XYChart.paint(XYChart.java:254)
    at org.knowm.xchart.XYChart.paint(XYChart.java:225)
    at org.knowm.xchart.XChartPanel.paintComponent(XChartPanel.java:95)
    at javax.swing.JComponent.paint(JComponent.java:1056)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5219)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1572)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1495)

Here is my code, where xyChart is declared as a field. I am convinced that it is the constructor part for XChartPanel gets wrong, but I simply don't know where to start.

/**
     * Initialize the XYChart
     */
    private void initXYChart() {
        // Create Chart
        xyChart = new XYChartBuilder().width(800).height(800).title(getClass().getSimpleName())
                .xAxisTitle(xColName)
                .yAxisTitle("Y").build();

        // Customize Chart
        xyChart.getStyler().setLegendPosition(LegendPosition.InsideNE);
        xyChart.getStyler().setAxisTitlesVisible(true);
        xyChart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Line);

        double[] yCoordArray = new double[xCoordArray.length];
        // Loop through the series
        if (!seriesList.isEmpty()) {
            for (int i = 0; i < yCoordinates.size(); i++) {
                List<Double> yCoordOneSeries = yCoordinates.get(i);
                // Convert list to array
                for (int j = 0; j < yCoordArray.length; j++) {
                    yCoordArray[j] = yCoordOneSeries.get(j);
                }
                xyChart.addSeries(yColNames.get(i), xCoordArray, yCoordArray);
            }
        }

        xyChartPanel = new XChartPanel<>(xyChart);

        add(xyChartPanel, BorderLayout.CENTER);
    }
xxx222
  • 2,980
  • 5
  • 34
  • 53

1 Answers1

0

The error is not in the code you posted.
As the error message reports, you try to access a method or variable of an object reference that is null.
This happens in Legend_AxesChart.paint() method in line 195 of Legend_AxesChart.java. So go there and look what could possibly be null.

Vampire
  • 35,631
  • 4
  • 76
  • 102