0

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>
Jaroslaw
  • 49
  • 1
  • 8
  • In which line of your code is the `NullPointerException` occurring? Is it this line: `chartParent = FXMLLoader.load(getClass().getResource("/eng/gascalculator/view/ChartsPane.fxml"));`? – Abra May 05 '19 at 08:24
  • No ,is the line with method "lineChart.getData().addAll(series); – Jaroslaw May 05 '19 at 08:46
  • Please [edit] your question to add a [mcve] demonstrating the issue. My guess is `lineChart` is null, but without a knowing more it's difficult to say why. Is the instance you're in supposed to be the controller used by `ChartsPane.fxml`? Because if it is, then know that, when using `fx:controller`, the loader will create and use a _new, separate_ instance of the controller class. – Slaw May 05 '19 at 08:54
  • Either `lineChart` is null, or the value returned by `lineChart.getData()` is null. My money is on `lineChart`. Where, in your code, do you assign a value to `lineChart`? I didn't see it in the code you posted. Maybe take @Slaw advice and post a [mcve]. – Abra May 05 '19 at 09:13
  • Based on the minimal example you (erroneously) put in [an answer](https://stackoverflow.com/a/55994110/6395627), the issue is definitely related to different instances of the class being used. However, the fundamental problem here is that you're sharing the same controller class with two different FXML files. Don't do this; each FXML file should have its own controller class. If you need to communicate between controllers, see [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963) and related/linked questions. – Slaw May 07 '19 at 09:22
  • @Slaw, thank you, i split the control into two controlers and now is working fine – Jaroslaw May 08 '19 at 17:08

0 Answers0