Need help in debugging this JavaFX program: The following program builds a sine
ID: 657169 • Letter: N
Question
Need help in debugging this JavaFX program:
The following program builds a sine wave using JavaFX and animates a ball moving left to right (uses KeyCode UP/DOWN to control play/pause. Keycode LEFT/RIGHT increases/decreases speed of the ball). Upon running the application, I am thrown a InvocationTargetException (somehow related to the lambda expression)
package application;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.SplitPane;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.CubicCurve;
import javafx.scene.shape.Line;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.QuadCurveTo;
import javafx.stage.Stage;
import javafx.util.Duration;
public class BallOnCurve3 extends Application implements
EventHandler {
public PathTransition pt;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Animation: Ball on Curve");
StackPane graph = new StackPane();
// create the curve
Path path = new Path();
MoveTo moveTo = new MoveTo();
moveTo.setX(0);
moveTo.setY(100);
QuadCurveTo p2 = new QuadCurveTo();
p2.setX(30f);
p2.setY(40f);
p2.setControlX(5f);
p2.setControlY(50f);
QuadCurveTo p3 = new QuadCurveTo();
p3.setX(60f);
p3.setY(100f);
p3.setControlX(50f);
p3.setControlY(40f);
QuadCurveTo p4 = new QuadCurveTo();
p4.setX(100f);
p4.setY(160f);
p4.setControlX(70f);
p4.setControlY(155f);
QuadCurveTo p5 = new QuadCurveTo();
p5.setX(130f);
p5.setY(100f);
p5.setControlX(125f);
p5.setControlY(155f);
QuadCurveTo p6 = new QuadCurveTo();
p6.setX(160f);
p6.setY(40f);
p6.setControlX(140f);
p6.setControlY(50f);
QuadCurveTo p7 = new QuadCurveTo();
p7.setX(200f);
p7.setY(100f);
p7.setControlX(180f);
p7.setControlY(30f);
QuadCurveTo p8 = new QuadCurveTo();
p8.setX(240f);
p8.setY(160f);
p8.setControlX(210f);
p8.setControlY(150f);
QuadCurveTo p9 = new QuadCurveTo();
p9.setX(280f);
p9.setY(100f);
p9.setControlX(270f);
p9.setControlY(160f);
path.getElements().add(moveTo);
path.getElements().add(p2);
path.getElements().add(p3);
path.getElements().add(p4);
path.getElements().add(p5);
path.getElements().add(p6);
path.getElements().add(p7);
path.getElements().add(p8);
path.getElements().add(p9);
path.setFill(new Color(1, 0, 0, 0));
path.setStrokeWidth(3);
Line hline = new Line();
hline.setStartX(0.0);
hline.setStartY(100.0);
hline.setEndX(280.0f);
hline.setEndY(100.0f);
Line vline = new Line();
vline.setStartX(130.0f);
vline.setStartY(0.0f);
vline.setEndX(130.0f);
vline.setEndY(200.0f);
//create the circle
Circle circle = new Circle(125, 100, 50);
circle.setFill(Color.WHITE);
circle.setStroke(Color.BLACK);
pt.setDuration(Duration.millis(4000));
pt.setPath(circle);
pt.setNode(path);
pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pt.setCycleCount(Timeline.INDEFINITE);
pt.setAutoReverse(true);
pt.play();
//Add objects
graph.getChildren().addAll(path, hline, vline, circle);
//Event Handler
circle.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.UP || e.getCode() == KeyCode.DOWN
|| e.getCode() == KeyCode.RIGHT
|| e.getCode() == KeyCode.LEFT) {
try {
if (e.getCode() == KeyCode.UP) {
pt.pause();
} else if (e.getCode() == KeyCode.DOWN) {
pt.play();
} else if (e.getCode() == KeyCode.LEFT) {
increaseSpeed();
} else if (e.getCode() == KeyCode.LEFT) {
decreaseSpeed();
}
} catch (Exception f) {
f.printStackTrace();
}
}
});
//Set panes on the stage
BorderPane layout = new BorderPane();
layout.setTop(graph);
layout.setBottom(getHBoxButtons());
// render the nodes
Scene scene = new Scene(layout, 300, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
//increase speed of the ball
public void increaseSpeed() {
pt.setRate(pt.getRate() + 0.5);
}
//decrease speed of the ball
public void decreaseSpeed() {
pt.setRate(pt.getRate() > 0 ? pt.getRate() - 0.1 : 0);
}
public HBox getHBoxButtons() {
HBox hBox = new HBox(15);
hBox.setPadding(new Insets(15, 5, 5, 5));
return hBox;
}
// Event Handler
@Override
public void handle(ActionEvent event) {
}
}
This is the exception that is thrown:
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$152(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/355629945.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at application.BallOnCurve3.start(BallOnCurve3.java:135)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/240635617.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/1681433494.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/205526685.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1685538367.run(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$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2058534881.run(Unknown Source)
... 1 more
Exception running application application.BallOnCurve3
Explanation / Answer
You might not put your fxml file in proper place so you are getting this NullPointerException
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.