我制作了一组图像,以设置较少数量的图像视图。我想要 10 个图像随机分配给 4 个图像视图,然后通过网格窗格显示。每次运行代码时都会出现错误,“运行应用程序时出现异常”。是图片的路径吗?我没有看到任何明显的错误。
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