为什么我不能在新窗口中向折线图添加XY点

时间:2019-05-05 08:20:51

标签: java javafx

我正在使用JavaFX,问题是当在XYChart中训练绘制新行时,出现错误NullPointerException。创建新的窗格后,它会变亮。 Secont Pane正在以折线图打开,但为空。我从以下代码的这一行得到错误。

lineChart.getData().addAll(series);   

当我尝试在主窗口中执行此操作时,一切正常,并且线条绘制正确。但是,当我在创建第二个窗格后尝试执行此操作时,出现错误NullPointerException。

代码片段向我发送错误:

    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);
        }

    });

它的工作原理是,当我单击图表按钮时,新窗格应打开,其中包含图表线和图表上的数据。

我认为这将是一个新的单独实例的问题,例如@Slaw tell。下面,我用最少的代码准备新程序。我有2个fxml文件都已将刺客控制到MainController类。 MainPane打开正确,但是当我按下按钮打开带有图表的第二个窗格时,我得到信息,lineChart为空。

MainConntroller类:

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);
        }

    });

}

}

主类:

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文件:

<?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文件:

<?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>

0 个答案:

没有答案