0

I'm desperate. What am I missing? I'm getting a NullPointerException in Class ChartTabPageController.java at lineChart.getData().add(series);

MainController

    /**
 * Shows the person overview inside the root layout.
 */
public void showPriceOverview() {
    try {
        // Load person overview.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class
                .getResource("view/PriceOverviewTab.fxml"));
        TabPane priceOverview = (TabPane) loader.load();

        // Set person overview into the center of root layout.
        rootLayout.setCenter(priceOverview);

        // loader.getController() liefert eine Referenz vom Typ
        // PersonOverviewController?
        // Give the controller access to the main app.
        PriceOverviewTabController controller = loader.getController();
        // Dem Controller wird die MainApp übergeben, sodass dieser auf die
        // ObservableList<Person> personData zugreifen kann
        // In dem Controller erfolgt dann die Zuweisung von
        // ObservableList<Person> und TableView<Person>
        controller.setMainApp(this);

    } catch (IOException e) {
        e.printStackTrace();
    }
}

Part of Middle Controller

/**
 * Is called by the main application to give a reference back to itself.
 * 
 * @param mainApp
 */
public void setMainApp(MainApp mainApp) {
    this.mainApp = mainApp;

    // Hier werden die "Daten" (ObservableList<PriceForward>) an den
    // Controller übergeben und den PriceTabele Instanzen über die Methode
    // setItems() zugewiesen.
    priceTable.setItems(mainApp.getPriceData());
    priceTable1.setItems(mainApp.getPriceData());

    chartTabPageController = new ChartTabPageController();
    chartTabPageController.setPriceData(mainApp.getPriceData());
}

Last Controller

public class ChartTabPageController {

    @FXML
    private LineChart<String, Double> lineChart;

    @FXML
    private CategoryAxis xAxis;

    private ObservableList<String> time = FXCollections.observableArrayList();

    /**
     * Initializes the controller class. This method is automatically called
     * after the fxml file has been loaded.
     */
    @FXML
    private void initialize() {

        // Assign the month names as categories for the horizontal axis.
        xAxis.setCategories(time);

    }

    /**
     * Sets the persons to show the statistics for.
     * 
     * @param persons
     */
    public void setPriceData(List<PriceForward> prices) {
        int j = 0;

        for (int i = 0; i < 96; i++) {
            time.add(LocalTime.of(0, 0, 0).plusMinutes(15 * i).toString());
        }

        Double[] price = new Double[prices.size()];

        for (PriceForward p : prices) {
            price[j] = p.getPriceToday();
            j++;
        }

        XYChart.Series<String, Double> series = new XYChart.Series<>();

        // Create a XYChart.Data object for each month. Add it to the series.
        for (int i = 0; i < prices.size(); i++) {

            series.getData().add(new XYChart.Data<>(time.get(i), price[i]));

        }

        lineChart.getData().add(series);
    }
}

I have no clue why I'm getting a NullPointerException at my last line of code. Hope you guy can help ...

James_D
  • 201,275
  • 16
  • 291
  • 322
Andrew
  • 67
  • 8
  • `@FXML`-annotated fields are initialized *in the controller* by the `FXMLLoader`. In your code, `chartTabPageController` is not the controller, it is just an object you create that happens to be the same class as the controller. – James_D Mar 15 '17 at 14:17
  • thanks very much, this worked out! – Andrew Mar 15 '17 at 15:27

0 Answers0