应用程序中的异常 - Javafx

时间:2016-02-20 18:13:04

标签: java javafx

为什么我在我的应用程序中遇到此异常?

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Children: child node is null: parent = Group@f4a634[styleClass=root]
    at javafx.scene.Parent$2.onProposedChange(Unknown Source)
    at com.sun.javafx.collections.VetoableListDecorator.add(Unknown Source)
    at MainClass.update(MainClass.java:36)
    at MainClass.start(MainClass.java:28)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    ... 1 more
Exception running application MainClass

问题在于这个功能:

 public void update(final Scene scene) {    
                final Group root = (Group) scene.getRoot(); 
                root.getChildren().add(chart);
                Timeline timeLine = new Timeline(Timeline.INDEFINITE, 
                        new KeyFrame(new Duration(100),
                        x -> {                          
                            while(t < 1) {
                                final double thisT = t;
                                chart = new Chart(e -> Math.exp(-(Math.pow((e-thisT ), 2)))*Math.cos((2*Math.PI*(e-thisT))/l),
                                        -1, 1, 0.01, new Axes(1000, 1000,
                                                -1, 1, 0.1, -1, 1, 0.1)
                                        );
                                t+=0.05;    
                            }     

                        }));            
                timeLine.setAutoReverse(true);
                timeLine.setCycleCount(1000);
                timeLine.play();

       }

结果应该是从左侧开始向右移动图表的动画淡入淡出,但如果我放置root.getChildren()。add(chart);在lambda expresion里面,我会得到另一个我想要的结果。 最后一步中的图表,第一步中的重复图表或图表。其中任何一个都没有动画。如果我强调核心地添加孩子应该在开头,时间线应该工作,如果我使用swing,java我会在每次更新后重新添加图表,但我不能那样做。现在这让我很困惑,因为我找不到好的解决方案。

1 个答案:

答案 0 :(得分:0)

您收到此异常是因为对象chart在行处为空:

  root.getChildren().add(chart);