0

As an assignment, I'm making a Slot Machine program in JavaFX.

If I do this:

GridPane grid = new GridPane();
grid.add(new Rectangle(20, 20), 0, 0);

it works. But if I do this:

GridPane grid = new GridPane();
Shape[] shapes = new Shape[6];
Rectangle square = new Rectangle(20, 20);
shapes[0] = square;
grid.add(shapes[0], 0, 0);

it will not. it returns the following compile-time error

    Executing D:\Libraries\Documents\NetBeansProjects\SlotMachine\dist\run624299409\SlotMachine.jar using platform C:\Program Files\Java\jdk1.8.0_60\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:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    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(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
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$156(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=50.0, vgap=50.0, alignment=CENTER
    at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
    at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
    at javafx.scene.layout.GridPane.add(GridPane.java:965)
    at slotmachine.SlotMachine.start(SlotMachine.java:71)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(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$149(WinApplication.java:191)
    ... 1 more
Exception running application slotmachine.SlotMachine
Java Result: 1

I've also tried to change the array type and declared it as Rectangle[], but still it won't compile.

I don't understand why. In shapes[0] I do have a Rectangle, why won't it allow me to add it to the grid?

EDIT: I'm using an array because once a Spin button is clicked, a random shape will be displayed (a random index from 0 to 5)

Uluk Biy
  • 48,655
  • 13
  • 146
  • 153

1 Answers1

1

The error says that you are adding the same child again to the grid pane. Check your code for re-adding. Most likely you constructed some loop and the error in that code.

Uluk Biy
  • 48,655
  • 13
  • 146
  • 153