我可以运行项目,但是一旦问题弹出,它就会中断

时间:2019-04-07 19:11:38

标签: java text

我不断收到: java.lang.NullPointerException错误消息。 the:healthDamage(selectedCardOption.GetHealthAdjustment());

然后错误显示“空指针访问:变量selectedCardOption只能在此位置为空” 我想我不确定为什么会继续破坏比赛。另外,我还要在哪里添加该选项以使其起作用?

我尝试制作一个局部变量字段,但没有用。

// Gather input from player

整数输入= scan.nextInt();

        // Showing Player Number They Selected
        System.out.println("You've Selected Option: " + input.toString());
        System.out.println("");

        // Process Card Option
        CardOption selectedCardOption = null;
        for (int i = 0; i < card.getOptions().size(); i++) {
            CardOption cardOption = card.getOptions().get(i);

            if (cardOption.GetInput() == input) {
                selectedCardOption = cardOption;
            }
        }
        // Only process if valid
        if (selectedCardOption != null) {
            if (selectedCardOption.GetIsExit()) {
                System.out.println("You've chosen to leave.. Thanks for playing!");
                running = false;
            }
        } else {
            // Adjust Health and Skill based upon selection
            healthDamage(selectedCardOption.GetHealthAdjustment());
            System.out.println(
                    "You've taken" + Integer.toString(selectedCardOption.GetHealthAdjustment()) + "Damage!");

            skillIncrease(selectedCardOption.GetSkillAdjustment());
            System.out.println(
                    "You've added" + Integer.toString(selectedCardOption.GetSkillAdjustment()) + "this time");
            // TODO: Logic to handle sub cards
        }
        if (selectedCardOption.getCards() != null && selectedCardOption.getCards().size() > 0) {
            Random r = new Random();
            int cardIndex = r.nextInt(selectedCardOption.getCards().size());
            card = selectedCardOption.getCards().get(cardIndex);
        }

该游戏应该引起一系列问题。它会在启动时中断。

0 个答案:

没有答案