正如标题所暗示的那样,我正在尝试使用JavaFx
制作一个以随机方式回答是或否的程序。我使用了一点Java
来实现它的随机方面。但是每次编译它都会产生异常。
这里是代码:
package decision2;
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.*;
import java.util.Random;
import javafx.geometry.*;
public class decision2 extends Application {
TextField question;
Label label;
Random rnd;
int rndnumber;
public static void main (String [] args) {
launch (args);
}
public void start (Stage myStage){
myStage.setTitle ("Decision Maker v1.0");
FlowPane node = new FlowPane (10,10);
node.setAlignment (Pos.CENTER);
Scene scene = new Scene (node,230,140);
myStage.setScene(scene);
question = new TextField();
question.setPromptText("Enter your question...");
question.setPrefColumnCount(15);
question.setOnAction(new EventHandler <ActionEvent>() {
public void handle (ActionEvent ae) {
rndnumber = rnd.nextInt (2);
//If the number is 1, it prints out yes, no otherwise.
if (rndnumber==1) {
label.setText("Yes!");
}
else {
label.setText("No.");
}
}
});
Button btn = new Button ("Ask Question");
btn.setOnAction (new EventHandler <ActionEvent> () {
public void handle (ActionEvent ae) {
rndnumber = rnd.nextInt (2);
if (rndnumber==1) {
label.setText("Yes!");
} else {
label.setText("No.");
}
}
});
Separator sp = new Separator ();
sp.setPrefWidth (180);
node.getChildren().addAll (question,btn,sp,label);
myStage.show();
}
}
这是编译输出
Executing C:\Users\EskimoOfHoth\Documents\NetBeansProjects\decision2\dist\run1520470956\decision2.jar using platform C:\Program Files\Java\jdk1.7.0_75\jre/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.javafx.main.Main.launchApp(Main.java:698)
at com.javafx.main.Main.main(Main.java:871)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Children: child node is null: parent = FlowPane@141567b[styleClass=root]
at javafx.scene.Parent$1.onProposedChange(Parent.java:316)
at com.sun.javafx.collections.VetoableObservableList.addAll(VetoableObservableList.java:106)
at com.sun.javafx.collections.ObservableListWrapper.addAll(ObservableListWrapper.java:160)
at com.sun.javafx.collections.ObservableListWrapper.addAll(ObservableListWrapper.java:309)
at decision2.decision2.start(decision2.java:58)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
... 1 more
Java Result: 1
答案 0 :(得分:1)
请初始化Label label
。当您尝试将其添加到null
时(以及当您尝试为标签设置文字时),它仍为FlowLayout
,