- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码给了我这个错误。现在,根据我阅读堆栈跟踪的有限知识,错误是由这行代码引起的:
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/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!