如何将文件中的数据转换为JavaFX LineChart中的系列文件?

时间:2017-01-16 04:47:28

标签: java javafx linechart

对于JavaFX LineChart,JavaFX图形功能有很多演示程序,如下所示:

public class LineChartSample extends Application {@Override public void start(Stage stage) {
    stage.setTitle("Line Chart Sample");
    //defining the axes
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("Number of Month");
    //creating the chart
    final LineChart<Number,Number> lineChart = 
            new LineChart<Number,Number>(xAxis,yAxis);

    lineChart.setTitle("Stock Monitoring, 2010");
    //defining a series
    XYChart.Series series = new XYChart.Series();
    series.setName("My portfolio");
    //populating the series with data
    series.getData().add(new XYChart.Data(1, 23));
    series.getData().add(new XYChart.Data(2, 14));
    //etc..

    Scene scene  = new Scene(lineChart,800,600);
    lineChart.getData().add(series);

    stage.setScene(scene);
    stage.show();
}public static void main(String[] args) {
    launch(args);
}

对我来说,限制因素是数据对必须在应用程序运行之前进行硬编码。我想改为使用从外部文件读取的数据来填充折线图,在我的例子中,这是一个包含数字数据的Excel电子表格。

此数据作为双精度数组列表检索。获取数据的方法在另一个包中,并且在其他应用程序中无错误地工作。该方法如下所示:

 private ArrayList<Double> teamstats;
 ...
 teamstats = ExcelManager.getTeamMavgStats("D1", "Arsenal");
 //iterate on teamstats to populate the series...

然而,当我介绍上面的代码行开始替换硬编码的getData()。添加调用时,我尝试从Netbeans 8.0.2运行程序时出现以下错误:

run:
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.    java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at     com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImp    l.java:389)
    at         com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.    java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start     method
at     com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:    917)
    at     com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(Launcher    Impl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at movavg.excel.ExcelManager.getTeamMavgStats(ExcelManager.java:163)
    at movavg.ui.LineChartSample.start(LineChartSample.java:73)
    at     com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(Launche    rImpl.java:863)
    at     com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.ja    va:326)
    at     com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295    )
    at java.security.AccessController.doPrivileged(Native Method)
    at    com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java    :294)
    at     com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:    95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at     com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    ... 1 more
Exception running application movavg.ui.LineChartSample
Java Result: 1

我发现很难知道该怎么做,因为我之前从未遇到过这种例外。

0 个答案:

没有答案