JavaFX InvocationTargetxception 和其他错误,试图显示图像
JavaFX InvocationTargetxception and other errors, trying to display image
我是编程方面的新手,所以如果这是一个新手问题,我深表歉意!我对 Java 有所了解,但我对 JavaFX 完全陌生。我正在尝试编写一个程序来打开 window 并在 GridPane 上显示一副 52 张卡片中的 3 张随机卡片图像,我似乎无法弄清楚为什么我不断收到 InvocationTargetException、RuntimeException 和ArrayIndexOutOfBoundsException。除了这些例外情况,我应该包括 try 和 catch 块吗?或者我应该包括其他例外情况吗?或者它可能是我的图像文件的位置?我当前所有52个卡片图像文件的位置是:C:\Users\Asus\Documents\NetBeansProjects\AdvancedJavaClass\src\Cards
以下是我的代码:
package Cards;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.util.Random;
public class ThreeCards extends Application {
@Override
public void start(Stage primaryStage) {
Image[] Cards = new Image[51];
ImageView firstCard = new ImageView();
ImageView secondCard = new ImageView();
ImageView thirdCard = new ImageView();
Random cardPicker = new Random();
int card1 = 0;
int card2 = 0;
int card3 = 0;
for(int i=1; i<53; i++) {
Cards[i-1] = new Image("file: "+i+".png");
}
if (card1 == card2 || card2 == card3 || card3 == card1) {
card1 = cardPicker.nextInt(51)+0;
card2 = cardPicker.nextInt(51)+0;
card3 = cardPicker.nextInt(51)+0;
}
firstCard.setImage(Cards[card1]);
secondCard.setImage(Cards[card2]);
thirdCard.setImage(Cards[card3]);
GridPane root = new GridPane();
root.getChildren().add(firstCard);
root.getChildren().add(secondCard);
root.getChildren().add(thirdCard);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Three Cards");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
以下是 happens/what 我在 运行 时得到的结果:
Executing C:\Users\Asus\Documents\NetBeansProjects\AdvancedJavaClass\dist\run2133970792\AdvancedJavaClass.jar using platform C:\Program Files\Java\jdk1.8.0_71\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$launchApplication5(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 51
at Cards.ThreeCards.start(ThreeCards.java:34)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(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$null8(WinApplication.java:191)
... 1 more
Exception running application Cards.ThreeCards
Java Result: 1
我目前使用的IDE是Netbeans。
请帮忙!我不明白为什么我会收到这些错误!谢谢!!
改变这个:
for(int i=1; i<53; i++) {
Cards[i-1] = new Image("file: "+i+".png");
}
收件人:
for(int i=0; i<51; i++) {
Cards[i] = new Image("file: "+ String.valueOf(i+1) +".png");
}
您的数组索引过多(Cards[51] 不存在)。
我是编程方面的新手,所以如果这是一个新手问题,我深表歉意!我对 Java 有所了解,但我对 JavaFX 完全陌生。我正在尝试编写一个程序来打开 window 并在 GridPane 上显示一副 52 张卡片中的 3 张随机卡片图像,我似乎无法弄清楚为什么我不断收到 InvocationTargetException、RuntimeException 和ArrayIndexOutOfBoundsException。除了这些例外情况,我应该包括 try 和 catch 块吗?或者我应该包括其他例外情况吗?或者它可能是我的图像文件的位置?我当前所有52个卡片图像文件的位置是:C:\Users\Asus\Documents\NetBeansProjects\AdvancedJavaClass\src\Cards
以下是我的代码:
package Cards;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.util.Random;
public class ThreeCards extends Application {
@Override
public void start(Stage primaryStage) {
Image[] Cards = new Image[51];
ImageView firstCard = new ImageView();
ImageView secondCard = new ImageView();
ImageView thirdCard = new ImageView();
Random cardPicker = new Random();
int card1 = 0;
int card2 = 0;
int card3 = 0;
for(int i=1; i<53; i++) {
Cards[i-1] = new Image("file: "+i+".png");
}
if (card1 == card2 || card2 == card3 || card3 == card1) {
card1 = cardPicker.nextInt(51)+0;
card2 = cardPicker.nextInt(51)+0;
card3 = cardPicker.nextInt(51)+0;
}
firstCard.setImage(Cards[card1]);
secondCard.setImage(Cards[card2]);
thirdCard.setImage(Cards[card3]);
GridPane root = new GridPane();
root.getChildren().add(firstCard);
root.getChildren().add(secondCard);
root.getChildren().add(thirdCard);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Three Cards");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
以下是 happens/what 我在 运行 时得到的结果:
Executing C:\Users\Asus\Documents\NetBeansProjects\AdvancedJavaClass\dist\run2133970792\AdvancedJavaClass.jar using platform C:\Program Files\Java\jdk1.8.0_71\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$launchApplication5(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 51
at Cards.ThreeCards.start(ThreeCards.java:34)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(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$null8(WinApplication.java:191)
... 1 more
Exception running application Cards.ThreeCards
Java Result: 1
我目前使用的IDE是Netbeans。 请帮忙!我不明白为什么我会收到这些错误!谢谢!!
改变这个:
for(int i=1; i<53; i++) {
Cards[i-1] = new Image("file: "+i+".png");
}
收件人:
for(int i=0; i<51; i++) {
Cards[i] = new Image("file: "+ String.valueOf(i+1) +".png");
}
您的数组索引过多(Cards[51] 不存在)。