为什么我在运行"时会遇到"异常错误?

时间:2015-04-06 03:17:52

标签: image javafx imageview gridpane

我制作了一个图像数组,用较少数量的图像视图进行设置。我希望10个图像随机分配给4个图像视图,然后通过网格窗格显示。每次运行代码时,我都会遇到错误,#34;异常运行应用程序"。它是图像的路径吗?我没有看到任何明显的错误。

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

这是日志

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

2 个答案:

答案 0 :(得分:0)

images[9] - 这不存在。该阵列只有九个元素。错误消息显示为:Caused by: java.lang.ArrayIndexOutOfBoundsException: 9

答案 1 :(得分:0)

问题是因为

Image [] images = new Image[9];

您正在初始化一个包含9个元素的数组,并尝试在其中插入10个元素。

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

代表9th index10th element。只需将数组大小增加到10

Image [] images = new Image[10];

如果您不确定数组的大小,更好的选择是使用ArrayList 。 ArrayList在元素添加时调整自身大小。

修改 - 根据用户评论

NullPointerException是因为您还没有初始化任何一个ImageView。在使用之前初始化ImageView。

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

或者您可以直接使用

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