0

im working on a problem for like 2 hours and I still cant fix it. My task is to programm 49 Rectangel Squares. so I tried to use a array to make it easier, but it doen'st want to work :'(

without using arrays it works..

Thank you for help!

source code:

 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

1 Answers1

0

You are getting a NullPointerException because you initialize an array, but forget to initialize the object in the array! q[0]-q[3] are all null. You cannot call the setFill() method on a null object.

change

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);

to something like

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);

Hope this helps.

Alexander
  • 2,449
  • 3
  • 27
  • 31