I'm sort of a newbie at programming so I apologize if this is sort of a newbie question! I've been exposed to Java somewhat but I'm completely new at JavaFX. I'm trying to write a program that will open a window and display 3 random card images from a deck of 52 cards on a GridPane and I can't seem to figure out why I keep getting InvocationTargetException, RuntimeException, and ArrayIndexOutOfBoundsException. Should I include try and catch blocks, with these exceptions? Or would there be other exceptions I should be including? Or could it be the location of my image files? My current location of all 52 card image files is: C:\Users\Asus\Documents\NetBeansProjects\AdvancedJavaClass\src\Cards
The following is my code:
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);
}
}
And the following is what happens/what I get when I run it:
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$launchApplication$155(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$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$null$148(WinApplication.java:191)
... 1 more
Exception running application Cards.ThreeCards
Java Result: 1
The IDE I am currently using is Netbeans. Please help! I can't figure out why I'm getting these errors! Thank you!!
Change this:
for(int i=1; i<53; i++) {
Cards[i-1] = new Image("file: "+i+".png");
}
To:
for(int i=0; i<51; i++) {
Cards[i] = new Image("file: "+ String.valueOf(i+1) +".png");
}
You are over-indexing your array (Cards[51] does not exist).