Error while adding ImageView[] array to BorderPane in JavaFX

Viewed 33

I made an ImageView array:

public static ImageView[] array = new ImageView[2];

Then set the ImageView:

ImageView img = new ImageView(new Image("sample.png"));
array [0] = img;
array [1] = img;

Then add it to the screen:

root.getChildren().add(array [0]);
root.getChildren().add(array [1]);

ERROR:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane@78e30575

So, where is my mistake?

1 Answers

The error message really says it all. You can't add the same child twice, and in your snippet both array[0] and array[1] point to the same object. You could, however, add two objects that use the same image:

array[0] = new ImageView(new Image("sample.png"));
array[1] = new ImageView(new Image("sample.png"));
Related