随机确定具有对象和一个对象的Arraylist(错误)

时间:2017-01-01 18:10:11

标签: java arrays object arraylist javafx

我想制作一个游戏,因此我需要创建一个带有对象的列表,每当我需要用新卡填充5个玩家卡的位置时,它应该从该列表中随机选择并且当我点击tha方法时卡应该运行,但我甚至没有一个对象的输出工作。当我取消注释它时,问题是下面标记的代码,当我按下开始按钮(运行标记的代码)时,它给我一个错误。请帮助他们,并提前感谢你!

编辑:由于评论,我解决了这个问题。我忘了调用方法“add();”我添加了对象,所以playerhand是空的“我也在代码中解决了它

我的控制器(每个fxml文档)

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();
    }
}

类玩家卡(我从要放入列表的对象中创建的类)

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 ;
    }
}

我的主要

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();
    }

}

这是我得到的错误

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

2 个答案:

答案 0 :(得分:0)

这是因为您的列表playerhand为空,因为您在致电add()之前没有致电output()

答案 1 :(得分:0)

您没有调用填充列表的add()方法。