0

I am trying to add default data into a linechart however I keep getting a null pointer exception.

My code is:

public class MainController {
    @FXML private LineChart<Number,Number> chtContrast;
    .
    .
    .
    private void setContrastChart() throws NullPointerException{
                        XYChart.Series<Number, Number> series1 = new XYChart.Series<>();
                        series1.setName("Series 1");
                        series1.getData().add(new XYChart.Data<>(0, 0));
                        series1.getData().add(new XYChart.Data<>(255, 255));
                        chtContrast.getData().add(series1);
        }
}

I have the system to initilize but the null pointer exception happens on the line chtContrast.getData().add(series1);

Ellis Thompson
  • 408
  • 6
  • 18
  • The obvious NPE to a non-JavaFX engineer is that `chtContrast` is null. What's *less* obvious to me is how that is expected to be injected. Are you *sure* that it isn't null when you invoke it? – Makoto Feb 11 '19 at 18:36
  • @Makoto I dont understand what you mean by _"it isn't null when you invoke it"_ – Ellis Thompson Feb 11 '19 at 18:38
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – ferpel Feb 11 '19 at 18:38
  • @ferpel the chart exists as the fxml is as thus `` and that loads fine – Ellis Thompson Feb 11 '19 at 18:42
  • To my point, someone's attempting to close this as a dupe of the venerable Java NPE exception. Yes, there is *something* that's null on that line, but it's unclear what that is. If you're sure that `chtContrast` is initialized on that line (e.g. verified by a debugger), then something *else* could be causing it. You sure `getData` doesn't return `null`? – Makoto Feb 11 '19 at 18:48

0 Answers0