我想製作一個遊戲,因此我需要創建一個包含對象的列表,並且每當我需要用一張新卡填充5張卡片時,它應該從列表中隨機選擇,當我點擊卡的方法應運行,但我甚至沒有一個對象的輸出工作。問題是當我取消註釋時,下面標記的代碼,當我按下開始按鈕(運行標記的代碼)時,它給我一個錯誤。請幫助他們,並提前謝謝你!對象和一個對象的Arraylist是隨機確定的(錯誤)
編輯:我解決了它,感謝評論。我忘了調用方法「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
可能重複[可能導致java.lang.reflect.InvocationTargetException?](http://stackoverflow.com/questions/6020719/what-could-cause-java-lang-reflect-invocationtargetexception) –
這是因爲你的'playerhand'是空的,這是因爲你沒有調用'add()' –