0

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!!

1 Answers1

0

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).

DVarga
  • 21,311
  • 6
  • 55
  • 60
  • Hi DVarga, and thanks for your reply! I did exactly what you told me to and changed the specified code, but I get the same exact ArrayIndexOutOfBounds Exception regardless! and it still refers to the same line (34), would you have any other ideas by any chance?? – needsomehelpls May 22 '16 at 04:34
  • Is line(34) this line: "Cards[i] = new Image("file: "+ String.valueOf(i+1) +".png");"? – DVarga May 22 '16 at 06:31
  • Yes, that is line (34) – needsomehelpls May 22 '16 at 14:47
  • Then if you declare it like "Image[] Cards = new Image[51];" and you insert the for loop I have written, it is impossible to get ArrayOutOfBoundsException :) – DVarga May 22 '16 at 16:50
  • Thanks for all your help DVarga! I fixed it, the problem was apparently something wrong with netbeans, because it would not make any arrays bigger than 51 indexes, despite the code. I had to make a new project with only the single class in it! Thank you for your help! – needsomehelpls May 24 '16 at 17:29
  • Could you please accept the answer if it was helpful? :) – DVarga May 24 '16 at 20:02