0

I wanted to make a game and therefor i needed to create a list with objects and whenever I need to fill the place of the 5 playercards with a new card it should get randomly chosen from that list and when I click tha method of the card should run but I not even the output of one Object works. The problem is the marked code below when I uncomment it, it gives me an error when I press on the start button (run the code that is marked). Please help em and thank you in advance!

EDIT: I solved it thanks to a comment. I forgot to call the method "add();" where I added the objects so playerhand was empty" I solved it in the code too

My Controller (of every fxml document)

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.ArrayList;
import java.util.Random;

public class Controller {

    playercard iceborn = new playercard(3, false, true, 0);
    playercard fireball = new playercard(5, true, false, 0);
    playercard warmogs = new playercard(0, false, false, 10);

    ArrayList<playercard> playerhand = new ArrayList<>();

    public void add() {
        playerhand.add(iceborn);
        playerhand.add(fireball);
        playerhand.add(warmogs);
    }

    public playercard output() {
        Random rand = new Random();
        int xzufallszahl = rand.nextInt(3);
        int zufallszahl = xzufallszahl/1;
        return playerhand.get(zufallszahl+1);
    }

    @FXML
    Stage stage;
    public Button Startgameb;

    @FXML
    public void Startgamebaction(ActionEvent event) throws IOException{

        /*************************************************
        /*the problem is I didnt called add(); to actually add the objects*/
        add(); /*this solves the problem*/
        output().activate();
        *************************************************/
        Parent root = FXMLLoader.load(getClass().getResource("Menu.fxml"));
        Pane root1 =  FXMLLoader.load(getClass().getResource("Tutorial.fxml"));
        Pane root2 = FXMLLoader.load(getClass().getResource("Game.fxml"));
        Scene tutorialscene = new Scene(root1, 900, 600);
        Scene gamescene = new Scene(root2, 900, 600);

        Image card = new Image("sample/card.jpg");
        ImageView firstplayercard = new ImageView();
        firstplayercard.setImage(card);
        firstplayercard.setFitWidth(120);
        firstplayercard.setFitHeight(160);
        firstplayercard.setLayoutX(50);
        firstplayercard.setLayoutY(430);

        ImageView secondplayercard = new ImageView();
        secondplayercard.setImage(card);
        secondplayercard.setFitWidth(120);
        secondplayercard.setFitHeight(160);
        secondplayercard.setLayoutX(175);
        secondplayercard.setLayoutY(430);

        ImageView thirdplayercard = new ImageView();
        thirdplayercard.setImage(card);
        thirdplayercard.setFitWidth(120);
        thirdplayercard.setFitHeight(160);
        thirdplayercard.setLayoutX(300);
        thirdplayercard.setLayoutY(430);

        ImageView fourthplayercard = new ImageView();
        fourthplayercard.setImage(card);
        fourthplayercard.setFitWidth(120);
        fourthplayercard.setFitHeight(160);
        fourthplayercard.setLayoutX(425);
        fourthplayercard.setLayoutY(430);

        ImageView fifthplayercard = new ImageView();
        fifthplayercard.setImage(card);
        fifthplayercard.setFitWidth(120);
        fifthplayercard.setFitHeight(160);
        fifthplayercard.setLayoutX(550);
        fifthplayercard.setLayoutY(430);

        root2.getChildren().addAll(firstplayercard, secondplayercard, thirdplayercard, fourthplayercard, fifthplayercard);

        firstplayercard.setOnMouseClicked(e ->{
            System.out.println("Hello world!");
        });

        Stage stage;
        stage=(Stage) Startgameb.getScene().getWindow();
        stage.setScene(gamescene);
        stage.show();
    }
}

Class playercard (the class I made from the object I want to put in the list)

package sample;

public class playercard {
    int dmgovertime;
    boolean fire;
    boolean ice;
    int getlife;
    int playerhp = 100;
    int enemyhp = 300;

    public playercard(int dmgovertime, boolean fire, boolean ice, int getlife) {
        this.dmgovertime = dmgovertime;
        this.fire = fire;
        this.ice = ice;
        this.getlife = getlife;
    }

    public void activate(){
        enemyhp = enemyhp - dmgovertime + getlife ;
    }
}

And my main

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("Menu.fxml"));
        Scene menuscene = new Scene(root, 900, 600);

        primaryStage.setTitle("Project: Spark!");
        primaryStage.setScene(menuscene);
        primaryStage.show();
    }

}

And this is the error I get

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: 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 sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 48 more
Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at sample.Controller.output(Controller.java:38)
    at sample.Controller.Startgamebaction(Controller.java:48)
    ... 58 more

Process finished with exit code 0
  • Possible duplicate of [What could cause java.lang.reflect.InvocationTargetException?](http://stackoverflow.com/questions/6020719/what-could-cause-java-lang-reflect-invocationtargetexception) – Anindya Dutta Jan 01 '17 at 18:17
  • It is because your `playerhand` is empty and that's because you didn't call `add()` – Shreyash S Sarnayak Jan 01 '17 at 18:18

2 Answers2

0

It is because your list playerhand is empty and that's because you didn't call add() before calling output()

Shreyash S Sarnayak
  • 2,309
  • 19
  • 23
0

You are not calling your add() method, that populates a list.

Orest Savchak
  • 4,529
  • 1
  • 18
  • 27