0

So basically the challenge is in the title. It's pretty basic but I think I have most of it. The one problem I am having is that the place from which the pictures of the cards are coming from. i know that usually you have them coming from your computer, but I am doing it where the pics are coming from an web URL. This is what i have so far but I am getting a host of errors(InvocationTargetException, Runtime, and IndexOutOfBounds) Any help is appreciated.

import java.util.ArrayList;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
public class Exercise14_03 extends Application{
    @Override
    public void start(Stage primaryStage){
        ArrayList<String> cards = new ArrayList<>();

        for(int i = 0; i < 52; i++){
            cards.add(String.valueOf(i + 1));

        java.util.Collections.shuffle(cards);

        ImageView view1 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(0) + ".png"));
        ImageView view2 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(1) + ".png"));
        ImageView view3 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(2) + ".png"));

        HBox root = new HBox();

        root.getChildren().add(view1);
        root.getChildren().add(view2);
        root.getChildren().add(view3);

        Scene scene = new Scene(root);

        primaryStage.setTitle("Exercise14_03");
        primaryStage.setScene(scene);
        primaryStage.show();
        }
    }

    public static void main(String[] args){
        launch(args);
    }
}

(EXCEPTIONS)

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:498)
    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:498)
    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.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at Exercise14_03.start(Exercise14_03.java:29)
    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)
Jmpollock56
  • 5
  • 1
  • 4

1 Answers1

2

The reason people use style guides for indentation, etc, is that it reduces the possibility for errors. If you take your code:

import java.util.ArrayList;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
public class Exercise14_03 extends Application{
    @Override
    public void start(Stage primaryStage){
        ArrayList<String> cards = new ArrayList<>();

        for(int i = 0; i < 52; i++){
            cards.add(String.valueOf(i + 1));

        java.util.Collections.shuffle(cards);

        ImageView view1 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(0) + ".png"));
        ImageView view2 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(1) + ".png"));
        ImageView view3 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(2) + ".png"));

        HBox root = new HBox();

        root.getChildren().add(view1);
        root.getChildren().add(view2);
        root.getChildren().add(view3);

        Scene scene = new Scene(root);

        primaryStage.setTitle("Exercise14_03");
        primaryStage.setScene(scene);
        primaryStage.show();
        }
    }

    public static void main(String[] args){
        launch(args);
    }
}

and indent it correctly (most IDEs will do this for you, e.g. in Eclipse you can select everything with CTRL-A and then format it with CTRL-SHIFT-F), you get

import java.util.ArrayList;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;

public class Exercise14_03 extends Application {
    @Override
    public void start(Stage primaryStage) {
        ArrayList<String> cards = new ArrayList<>();

        for (int i = 0; i < 52; i++) {
            cards.add(String.valueOf(i + 1));

            java.util.Collections.shuffle(cards);

            ImageView view1 = new ImageView(
                    new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(0) + ".png"));
            ImageView view2 = new ImageView(
                    new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(1) + ".png"));
            ImageView view3 = new ImageView(
                    new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(2) + ".png"));

            HBox root = new HBox();

            root.getChildren().add(view1);
            root.getChildren().add(view2);
            root.getChildren().add(view3);

            Scene scene = new Scene(root);

            primaryStage.setTitle("Exercise14_03");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Now the problem is obvious: almost your entire start(...) method is inside your for loop. On the first iteration of the loop, you add the first element, then try to access elements 0, 1, and 2, causing the IndexOutOfBoundsException.

Just move the code out of the for loop:

import java.util.ArrayList;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;

public class Exercise14_03 extends Application {
    @Override
    public void start(Stage primaryStage) {
        ArrayList<String> cards = new ArrayList<>();

        for (int i = 0; i < 52; i++) {
            cards.add(String.valueOf(i + 1));
        }

        java.util.Collections.shuffle(cards);

        ImageView view1 = new ImageView(
                new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(0) + ".png"));
        ImageView view2 = new ImageView(
                new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(1) + ".png"));
        ImageView view3 = new ImageView(
                new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(2) + ".png"));

        HBox root = new HBox();

        root.getChildren().add(view1);
        root.getChildren().add(view2);
        root.getChildren().add(view3);

        Scene scene = new Scene(root);

        primaryStage.setTitle("Exercise14_03");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

It is also useful to learn how to read the stack trace.

Community
  • 1
  • 1
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Idk why I didn't see that haha, and yeah I think I'm still getting used to the copy and pasting of stackoverflow because it looks the same as what you posted on my end. Thank you for your help! – Jmpollock56 Feb 23 '17 at 15:41
  • 1
    @Jmpollock56 You didn't see it because it's not indented correctly, and because you didn't bother reading the error message. – James_D Feb 23 '17 at 15:42