StackedAreaChart用空系列锁定

时间:2014-11-09 23:11:10

标签: javafx stacked-area-chart

我遇到了

java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.elementData(ArrayList.java:418)

应用程序中的问题,数据集有时可能为空(来自SQL结果集),在本例中为seriesMay:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedAreaChart;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class StackedAreaChartSample2 extends Application {
    final NumberAxis xAxis = new NumberAxis(1, 24, 1);
    final NumberAxis yAxis = new NumberAxis(0,100,1);
    final StackedAreaChart<Number, Number> chart =
        new StackedAreaChart<>(xAxis, yAxis);
    final AnchorPane ap = new AnchorPane();

    @Override
    public void start(Stage stage) {
        stage.setTitle("Area Chart Sample");
        chart.setTitle("Temperature Monitoring (in Degrees C)");
        final XYChart.Series<Number, Number> seriesApril = new XYChart.Series<Number, Number>();
        seriesApril.setName("April");
        seriesApril.getData().add(new XYChart.Data<Number, Number>(1, 4));
        XYChart.Series<Number, Number> seriesMay =  new XYChart.Series<>();
        seriesMay.setName("May");

        chart.getData().addAll(seriesApril, seriesMay);

        double border= 15.0;
        ap.getChildren().add(chart);
        AnchorPane.setTopAnchor(chart, border);
        AnchorPane.setBottomAnchor(chart, border);
        AnchorPane.setLeftAnchor(chart, border);
        AnchorPane.setRightAnchor(chart, border);
        Scene scene = new Scene(ap, 800, 600);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

导致以下堆栈跟踪:

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:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
    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:483)
    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:875)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda$48/128893786.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.elementData(ArrayList.java:418)
    at java.util.ArrayList.get(ArrayList.java:431)
    at javafx.scene.chart.StackedAreaChart.layoutPlotChildren(StackedAreaChart.java:661)
    at javafx.scene.chart.XYChart.layoutChartChildren(XYChart.java:728)
    at javafx.scene.chart.Chart$1.layoutChildren(Chart.java:88)
    at javafx.scene.Parent.layout(Parent.java:1074)
    at javafx.scene.Parent.layout(Parent.java:1080)
    at javafx.scene.Parent.layout(Parent.java:1080)
    at javafx.scene.Scene.doLayoutPass(Scene.java:532)
    at javafx.scene.Scene.preferredSize(Scene.java:1612)
    at javafx.scene.Scene.impl_preferredSize(Scene.java:1686)
    at javafx.stage.Window$9.invalidated(Window.java:765)
    at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109)
    at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:143)
    at javafx.stage.Window.setShowing(Window.java:841)
    at javafx.stage.Window.show(Window.java:856)
    at javafx.stage.Stage.show(Stage.java:255)
    at StackedAreaChartSample2.start(StackedAreaChartSample2.java:41)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$78/255354427.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/1051754451.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/840359454.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/1775282465.run(Unknown Source)
    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$141(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
    ... 1 more
Exception running application StackedAreaChartSample2

更改
chart.getData().addAll(seriesApril, seriesMay);

chart.getData().addAll(seriesMay,seriesApril);

工作正常,每个人本身也能正确显示。我猜这里有一个bug ...

  

java版&#34; 1.8.0_25&#34; Java(TM)SE运行时环境(构建   1.8.0_25-b18)Java HotSpot(TM)64位服务器VM(内置25.25-b02,混合模式)

我试了半个小时左右来报告但是在某个时候放弃了 - 也许是某个志愿者?

1 个答案:

答案 0 :(得分:0)

如果您将seriesMay设置为final:

final XYChart.Series<Number, Number> seriesMay =  new XYChart.Series<>();

工作正常。