0

I made an array of Images to be set with a lesser number of image views. I want 10 images to randomly assign themselves to 4 Image views and then be displayed through the gridpane. Every time I run the code I get an error, "exception while running application". Is it the path of the images? I don't see any obvious errors.

package Flag;
 import java.util.Random;
 import javafx.application.Application;
 import javafx.geometry.Pos;
 import javafx.scene.Scene; 
 import javafx.scene.image.Image;
 import javafx.scene.image.ImageView;
 import javafx.scene.layout.GridPane;
 import javafx.stage.Stage; 

 public class Flag extends Application {

     @Override public void start(Stage primaryStage) {
         // Initialize Variables
         GridPane pane = new GridPane();
         pane.setAlignment(Pos.CENTER);
         ImageView [] imv = new ImageView [4];
         Image [] images = new Image[10];

         //Fill Images array
         images[0] = new Image(Flag.class.getResourceAsStream("images/flag0.gif"));
         images[1] = new Image(Flag.class.getResourceAsStream("images/flag1.gif"));    
         images[2] = new Image(Flag.class.getResourceAsStream("images/flag2.gif"));
         images[3] = new Image(Flag.class.getResourceAsStream("images/flag3.gif"));
         images[4] = new Image(Flag.class.getResourceAsStream("images/flag4.gif"));
         images[5] = new Image(Flags.class.getResourceAsStream("images/flag5.gif"));    
         images[6] = new Image(Flag.class.getResourceAsStream("images/flag6.gif"));
         images[7] = new Image(Flag.class.getResourceAsStream("images/flag7.gif"));
         images[8] = new Image(Flags.class.getResourceAsStream("images/flag8.gif"));
         images[9] = new Image(Flags.class.getResourceAsStream("images/flag9.gif"));

          //Random number
        Random rand = new Random();

         //Give Each Image an Image View
         for (ImageView imv1 : imv) {
/*This is line 38*/  imv1.setImage(images[rand.nextInt(9)]);
         } 

      // Add nodes to pane
         pane.add(imv[0], 0, 0);
         pane.add(imv[1], 0, 1);
         pane.add(imv[2], 1, 0);
         pane.add(imv[3], 1, 1);

     //Create a scene and place it in the stage
        Scene scene = new Scene(pane);
        primaryStage.setTitle("ShowGridPane");
        primaryStage.setScene(scene);
        primaryStage.show();
     }

     public static void main(String[] args) {
         Application.launch(args);
     }
 }

Here's the log

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:642)
    at com.javafx.main.Main.main(Main.java:805)
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:724)
Caused by: java.lang.NullPointerException
    at Flag.Flag.start(FlagsHwB.java:38)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
    ... 1 more
Java Result: 1
user3543798
  • 35
  • 1
  • 6

2 Answers2

0

images[9] - this does not exist. The array only has nine elements. The error message says this: Caused by: java.lang.ArrayIndexOutOfBoundsException: 9

0

The problem is because of

Image [] images = new Image[9];

You are initializing an array of 9 elements and trying to insert 10 elements into it.

images[9] = new Image(Flags.class.getResourceAsStream("images/flag9.gif"));

represents the 9th index and the 10th element. Just increase the array size to 10 i.e.

Image [] images = new Image[10];

A better option is to use an ArrayList if you are not sure of the size of the array. ArrayList resizes itself on element adding.

Edit - as per user comments

The NullPointerException is because you haven't initialized any of you ImageView's. Initialize the ImageView's before using them.

for (ImageView imv1 : imv) {
    imv1 = new ImageView(); // Initialization
    imv1.setImage(images[rand.nextInt(9)]);
} 

or you can directly use

for (ImageView imv1 : imv) {
    imv1 = new ImageView(images[rand.nextInt(9)]));
} 
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176