gpt4 book ai didi

java - 空根引起的应用程序启动方法异常

转载 作者:行者123 更新时间:2023-12-02 03:16:08 25 4
gpt4 key购买 nike

我的代码给了我这个错误。现在,根据我阅读堆栈跟踪的有限知识,错误是由这行代码引起的:

primaryStage.setScene(new Scene(vb, 400, 300));`

错误为“根不能为空”。由于根是 vb,考虑到我初始化了它,我不明白它怎么可能为空。任何帮助将不胜感激。

  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

最佳答案

您没有初始化名为 vb 的字段:您在 init() 方法中声明了一个名为 vb 的局部变量(也) ,并初始化它。

替换

VBox vb = new VBox();

vb = new VBox();

关于java - 空根引起的应用程序启动方法异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40268648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com