Fatal error: Uncaught Error: Call to a member function getFaceValue() on null

Fatal error: Uncaught Error: Call to a member function getFaceValue() on null

我刚开始使用 php oop 并且 运行 出错了。我似乎无法自己找到解决方案。 错误是:

Fatal error: Uncaught Error: Call to a member function getFaceValue() on null in C:\xampp\htdocs\dicegame\Game.php:26 Stack trace: #0 C:\xampp\htdocs\dicegame\index.php(34): Game->play('3') #1 {main} thrown in C:\xampp\htdocs\dicegame\Game.php on line 26.

如果您知道答案或有任何提示,请告诉我!

index.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Dice Game </title>
        <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>

    <body>
        <div id="container">    
            <header>
                <?php
                ?>
            </header>

            <form action="index.php" method="POST">
                <input type="number" name="quantity" value="3" id="quantity" min="3" max="8" placeholder="Number of dice"></br>
                <input type="submit" name="throw" id="throwBtn" value="Roll">
            </form>


            <?php 
                include('NumberToThrow.php');
                include('Dice.php');
                include('Game.php');

                $quantity = $_POST["quantity"] ?? "";

                // start the game
                if(isset($_POST['throw'])) {

                    echo "<h3>Throw the dices, baby ................. <br/></h3>";
                    $myGame = new Game();
                    $myGame->play($quantity);
                }

            ?>
        </div>
        <footer ></footer>
    </body>
</html>
game.php

<?php 
    class Game {
        // class variables
        private $diceValue;         
        private $throwNumber;       
        private $count;
        private $dicesArray;    


        public function play($quantity) {
            
            $this->createThrowNumber();
            $this->count = 0;
            
            $this->dicesArray = new SplFixedArray(8);       // create array with length initially equals to 8
            for ($index = 0; $index < $quantity; $index++)
            {
                $dice = new Dice();                             // instantiate dice object
                $dice->throwDice();                             // make the throw
                $this->dicesArray[$index] = $dice;
            }
        
            foreach($this->dicesArray as $dice) // The code in this foreach could also be implemented in
                                                // the for-loop above, but for example these are seperated
            {           
                $this->diceValue = $dice->getFaceValue();       // get the result of the throw & assign it to $diceValue
                echo("</br>");
                echo "The value of the dice: " . $this->diceValue . " ";
                $this->showDiceImage($this->diceValue);         // show value of throw as an image 
                echo("</br>");
                if ($this->diceValue == $this->throwNumber) $this->count++;
            }

            
            // display statistics
            if($this->count > 0) {
                echo "</br>Number thrown: " . $this->count . " times.</br>";
            }
            else {
                echo "</br>Number is not thrown.</br>";
            }
        }


        public function createThrowNumber() {
            $numberObject = new NumberToThrow();    // instantiate object - instance of the NumberToThrow class
            $this->throwNumber = $numberObject->makeAThrow(); // make a number to throw
            echo "<p>Your number to throw: <b>" . $this->throwNumber . "</b></p>";
            $this->showDiceImage($this->throwNumber);   // show the number to throw as dice image
            echo "</br>";
        }


        public function showDiceImage($dots) {
            switch($dots) {
                case 1:
                    echo "<img class='dotsImg' src='images/d1.png'>";
                    break;
                case 2:
                    echo "<img class='dotsImg' src='images/d2.png'>";
                    break;
                case 3:
                    echo "<img class='dotsImg' src='images/d3.png'>";
                    break;
                case 4:
                    echo "<img class='dotsImg' src='images/d4.png'>";
                    break;
                case 5:
                    echo "<img class='dotsImg' src='images/d5.png'>";
                    break;
                case 6:
                    echo "<img class='dotsImg' src='images/d6.png'>";
                    break;
                default:
                    break;
            }
        }

    }
?>
Dice.php

<?php 
    class Dice {
        const NUMBER_OF_SIDES = 6;
        private $faceValue;

        public function throwDice() {
            $this->faceValue = rand(1, self::NUMBER_OF_SIDES);  // 1 & NUMBER_OF_SIDES are inclusive values
        }

        public function getFaceValue() {
            return $this->faceValue;
        }
    }
    
?>

当你创建 $this->dicesArray = new SplFixedArray(8); 它已经是一个包含 8 个索引的数组,这些索引的值为 NULL 如果你没有用你的对象填充所有索引,假设 public function play($quantity) 被调用$quantity = 6,因此,您将得到最后一个值为 NULL https://i.imgur.com/CWAtSlZ.png 的数组。希望现在您知道如何修复它。