0
私のコードは私にこのエラーを与えます。アプリケーション開始メソッドの例外がヌルルートによって発生しました
primaryStage.setScene(new Scene(vb, 400, 300));`
「ルートはNULLにすることはできません」というエラー:今すぐスタックはエラーをトレース読んで、私の限られた知識から、このコード行によって原因があります。ルートがvbであると、私はそれを初期化したと考えて、どのようにnullになるのか理解していません。どんな助けでも大歓迎です。
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import java.text.DecimalFormat;
// class definition
public class Projectile extends Application {
// init method
public void init() {
// Projectile Type - ComboBox
VBox vb = new VBox();
projectile_type_combobox = new ComboBox<String>();
projectile_type_label = new Label("Projectile Type");
projectile_type_combobox.getItems().addAll("Human", "Piano");
// Inital Speed ToggleGroup
initial_speed_toggleGroup = new ToggleGroup();
label_display = new Label("Radio Button 1 selected");
initial_speed_slow = new RadioButton("Slow");
initial_speed_medium = new RadioButton("Medium");
initial_speed_fast = new RadioButton("Fast");
initial_speed_slow.setToggleGroup(tg_radiobutton_group);
initial_speed_slow.setToggleGroup(tg_radiobutton_group);
initial_speed_fast.setToggleGroup(tg_radiobutton_group);
// use the .setUserData command of the radio button to store speeds
initial_speed_slow.setUserData("10");
initial_speed_medium.setUserData("50");
initial_speed_fast.setUserData("70");
// Mass
mass_textField = new TextField();
mass_label = new Label("Mass(kgs)");
// Angle
angle_textField = new TextField();
angle_label = new Label("Angle(Degrees)");
// Speed
intitial_speed_textField = new TextField();
initial_speed_label = new Label("Initial Speed");
// Range
range_textField = new TextField();
range_label = new Label();
// Height
height_textField = new TextField();
height_label = new Label();
// Time
time_label = new Label();
time_textField = new TextField();
// Button
fire_button = new Button("Fire");
erase_button = new Button("Erase");
// Prevent the following TextFields from being editable: angle,intial
// speed range, height, time
// setEditable(false);
// Layout controls as per the diagram, feel free to improve the UI.
// How many rows and columns do you want - work this out on paper first
// My version has 7 rows, you can look at the JavaFX API to see how to
// get controls to span more than one column
gp = new GridPane();
gp.addRow(0, projectile_type_label, projectile_type_combobox);
gp.addRow(1, mass_textField, mass_label);
gp.addRow(2, angle_label, angle_textField);
gp.addRow(3, initial_speed_label, intitial_speed_textField);
gp.addRow(4, range_label, range_textField);
gp.addRow(5, height_label, height_textField);
gp.addRow(6, time_label, time_textField);
gp.addRow(7, fire_button, erase_button);
// Method call (not declaration!) to initialize the controls based on
// the projectile type.
initalizeControlValues();
// Listener for angle Slider to set angle TextTield and the angle
// variable
angle_slider.valueProperty().addListener(new ChangeListener<Number>() {
public void changed(final ObservableValue<? extends Number> observable, final Number oldValue,
final Number newValue) {
angle = (double) newValue;
angle_textField.setText("" + newValue);
}
});
// Listener for inital_speed ToggleGroup to set initital_speed TextField
this.initial_speed_toggleGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
public void changed(ObservableValue<? extends Toggle> ov, Toggle toggle, Toggle new_toggle) {
intitial_speed_textField.setText("" + new_toggle);
}
});
vb.getChildren().addAll(projectile_type_combobox, projectile_type_label, label_display, initial_speed_slow,
initial_speed_medium, initial_speed_fast, mass_textField, mass_label, angle_textField, angle_label,
intitial_speed_textField, initial_speed_label, range_textField, range_label, height_textField,
height_label, time_label, gp, time_textField, fire_button, erase_button);
// Listener to call the fire() method when the fire button is pressed
fire_button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
fire();
}
});
// Listener to initialize control values if the projectile type is
// changed
// Listener to initialize control values if the erase button is pressed
// erase_button.setOnAction(new EventHandler<ActionEvent>() {
// @Override
// public void handle(ActionEvent event) {
// erase();
// }
// });
}
// Overridden start method
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Projectile");
primaryStage.setScene(new Scene(vb, 400, 300));
primaryStage.show();
}
// Overridden stop method add functionality to this if you wish.
public void stop() {
}
// Entry point to our program
public static void main(String[] args) {
launch(args);
}
// Method to harvest values from controls, perform calculation and display
// the results
private void fire() {
// capture the values from the text fields outputting number errors
// where relevant
// don't forget to convert your angle input to radians for use with
// Math.sin()
// calculate the range of the projectile
range = ((initial_speed * initial_speed)/gravitational_accelleration) * Math.sin(2 * angle);
// calculate the flight time of the projectile
time = ((2 * initial_speed) * Math.sin(angle))/gravitational_accelleration;
// calculate the max height of the projectile
height = ((initial_speed * initial_speed) * Math.sin(angle))/2 * gravitational_accelleration;
// display the results in the relevant TextFields
}
private void erase() {
if (projectile_type_combobox.getValue() == "Human") {
mass = 80;
// Set slider scale 0 to 90, set slider value to 45 and ticks to
// units
angle_slider = new Slider(0, 45, 90);
angle_slider.setShowTickMarks(true);
angle_slider.setShowTickLabels(true);
angle_slider.setMajorTickUnit(15);
angle_slider.setBlockIncrement(0.5);
// initalize the intital speed to fast
this.initial_speed_fast.setSelected(true);
this.intitial_speed_textField.setText((String) this.initial_speed_fast.getUserData());
}
else {
// inital the mass to 400kg
mass = 400;
// Set slider scale 0 to 40, set slider value to 20 and ticks to 10
// units
angle_slider = new Slider(0, 20, 40);
angle_slider.setShowTickMarks(true);
angle_slider.setShowTickLabels(true);
angle_slider.setMajorTickUnit(10);
angle_slider.setBlockIncrement(0.5);
// initalize the intial speed to slow
this.initial_speed_slow.setSelected(true);
this.intitial_speed_textField.setText((String) this.initial_speed_slow.getUserData());
}
}
// Method to initalize the controls based on the selection of the projectile
// type
private void initalizeControlValues() {
if (projectile_type_combobox.getValue() == "Human") {
mass = 80;
// Set slider scale 0 to 90, set slider value to 45 and ticks to
// units
angle_slider = new Slider(0, 45, 90);
angle_slider.setShowTickMarks(true);
angle_slider.setShowTickLabels(true);
angle_slider.setMajorTickUnit(15);
angle_slider.setBlockIncrement(0.5);
// initalize the intital speed to fast
this.initial_speed_fast.setSelected(true);
this.intitial_speed_textField.setText((String) this.initial_speed_fast.getUserData());
}
else {
// inital the mass to 400kg
mass = 400;
// Set slider scale 0 to 40, set slider value to 20 and ticks to 10
// units
angle_slider = new Slider(0, 20, 40);
angle_slider.setShowTickMarks(true);
angle_slider.setShowTickLabels(true);
angle_slider.setMajorTickUnit(10);
angle_slider.setBlockIncrement(0.5);
// initalize the intial speed to slow
this.initial_speed_slow.setSelected(true);
this.intitial_speed_textField.setText((String) this.initial_speed_slow.getUserData());
}
// display ticks etc
// clear the results fields and variables
// The following variables SHOULD be initialized where appropriate as
// declaring and
// initializing separately is very verbose.
}
// Textfield
// Slider
private Label sli_display;
private Slider sli_slider;
// Radio Buttons
private RadioButton rb1, rb2, rb3;
private ToggleGroup tg_radiobutton_group;
private Label label_display;
private VBox vb;
// Layout
private GridPane gp;
// Projectile Type
private Label projectile_type_label;
private ComboBox<String> projectile_type_combobox;
// Mass
private Label mass_label;
private TextField mass_textField;
private double mass;
// Angle
private Label angle_label;
private Slider angle_slider;
private TextField angle_textField;
private double angle;
// Formating the values in the duration box
DecimalFormat df;
// Initial Speed
private Label initial_speed_label;
private ToggleGroup initial_speed_toggleGroup;
private RadioButton initial_speed_slow;
private RadioButton initial_speed_medium;
private RadioButton initial_speed_fast;
private TextField intitial_speed_textField;
private double initial_speed;
// Range
private Label range_label;
private TextField range_textField;
private double range;
// Height
private Label height_label;
private TextField height_textField;
private double height;
// Time
private Label time_label;
private TextField time_textField;
private double time;
// Gravity
private static final double gravitational_accelleration = 9.81; // m/s/s
// Calculate
private Button fire_button;
private Button erase_button;
}
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(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
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(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Root cannot be null
at javafx.scene.Scene.<init>(Scene.java:336)
at javafx.scene.Scene.<init>(Scene.java:223)
at Projectile.start(Projectile.java:171)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
... 1 more
Exception running application Projectile