I'm using JavaFX, problem is when traing drawing new line in XYChart, I get error NullPointerException. Its hapen after create a new Pane. Secont Pane is opening with line chart but is empty. I get the error from this line of the below code.
lineChart.getData().addAll(series);
When I trying to do this in primary window everything is going fine and the line is drawing correctly. But when i trying to do this after i create a second pane i get a error NullPointerException.
Fragment of code sending me a error:
chartsButton.setOnAction(new EventHandler<ActionEvent>() {
@SuppressWarnings("unchecked")
@Override
public void handle(ActionEvent event) {
Parent chartParent;
try {
Stage chartsWindow = new Stage();
chartParent = FXMLLoader.load(getClass().getResource("/eng/gascalculator/view/ChartsPane.fxml"));
chartsWindow.setTitle("Charts");
chartsWindow.setScene(new Scene(chartParent, 450, 450));
chartsWindow.show();
} catch (IOException e) {
}
XYChart.Series<String, Number> series = new XYChart.Series<String, Number>();
series.getData().add(new XYChart.Data<String, Number>("Jan", 10));
series.getData().add(new XYChart.Data<String, Number>("Feb", 30));
lineChart.getData().addAll(series);
}
});
Its should works like, when i click a chart Button, new pane should open with chart line and data on the chart.
I think this will be a problem with a new separate instance, like @Slaw tell. Below I prepare new program with minimal code. I have 2 fxml file both have controlled assassin to MainController class. MainPane open correct but when I push button to open second Pane with chart i get information the lineChart is null.
MainConntroller class:
package application;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class MainController implements Initializable {
@FXML
private Button chartButton;
@FXML
private LineChart<String, Number> lineChart;
@Override
public void initialize(URL location, ResourceBundle resources) {
chartButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Parent chartParent;
try {
Stage chartsWindow = new Stage();
chartParent = FXMLLoader.load(getClass().getResource("/application/ChartPane.fxml"));
chartsWindow.setTitle("Charts");
chartsWindow.setScene(new Scene(chartParent, 450, 450));
chartsWindow.show();
} catch (IOException e) {
}
XYChart.Series<String, Number> series = new XYChart.Series<String, Number>();
series.getData().add(new XYChart.Data<String, Number>("Jan", 10));
series.getData().add(new XYChart.Data<String, Number>("Feb", 30));
lineChart.getData().addAll(series);
}
});
}
}
Main class:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent parent = FXMLLoader.load(getClass().getResource("/application/MainPane.fxml"));
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.setTitle("First window");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
MainPane.fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/10.0.1"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<Button fx:id="chartButton" layoutX="265.0" layoutY="188.0"
mnemonicParsing="false" text="Chart" />
</children>
ChartPane.fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/10.0.1"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<LineChart fx:id="lineChart" layoutX="50.0">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</LineChart>
</children>
</AnchorPane>