我正在处理一个问题2个小时,我仍然无法修复它。 我的任务是编制49个Rectangel Squares。 所以我尝试使用数组使其更容易,但它确实想要工作:'(
不使用数组它可以工作..
谢谢你的帮助!
源代码:
package Game;
import javafx.scene.shape.Rectangle;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Game extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
VBox vb = new VBox();
VBox vb2 = new VBox();
HBox hb = new HBox();
Rectangle q[] = new Rectangle[4];
q[0].setFill(Color.RED);
q[1].setFill(Color.BLUE);
q[2].setFill(Color.GREEN);
q[3].setFill(Color.YELLOW);
vb.getChildren().addAll(q[0],q[1]);
vb2.getChildren().addAll(q[2],q[3]);
hb.getChildren().addAll(vb,vb2);
Scene sz1 = new Scene(hb);
primaryStage.setScene(sz1);
primaryStage.show();
}
}
my compiler :
> 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)
atcom.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Launcher
Impl.java:389)
atcom.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.jav
a: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
atcom.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.ja
va:917)
atcom.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(Launc
herImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at Spiel.FensterSpiel.start(FensterSpiel.java:29)
atcom.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Laun
cherImpl.java:863)
atcom.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl
.java:326)
atcom.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:
295)
at java.security.AccessController.doPrivileged(Native Method)
atcom.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.j
ava:294)
atcom.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.ja
va:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
atcom.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:19
1)
... 1 more
答案 0 :(得分:0)
你得到一个NullPointerException
,因为你初始化一个数组,但忘记初始化数组中的对象! q[0]
- q[3]
都是null
。您无法在setFill()
对象上调用null
方法。
变化
Rectangle q[] = new Rectangle[4];
q[0].setFill(Color.RED);
q[1].setFill(Color.BLUE);
q[2].setFill(Color.GREEN);
q[3].setFill(Color.YELLOW);
类似
Rectangle q[] = new Rectangle[4];
// initialize the rectangles
q[0] = new Rectangle();
q[1] = new Rectangle();
q[2] = new Rectangle();
q[3] = new Rectangle();
q[0].setFill(Color.RED);
q[1].setFill(Color.BLUE);
q[2].setFill(Color.GREEN);
q[3].setFill(Color.YELLOW);
希望这有帮助。