1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

構造体の作成(もう一度挑戦)⇒Uncaught TypeError: Cannot assign null to property がでる

Posted at

今Paizaラーニングを解いていて、もはやCレベルには達しており、
pythonも扱えるようになってきたので、Cクラスの残りの部分はもうストップして
Bクラスの問題集に行こうと思います。

前回は、下記でやっていますが下手すぎたので
再度やります。

まず構造体ってなんだ?というところから始めたので、
そこらへんは前回のを見て下さい。
ただ、PHPのコードは修正します。

<?php
    class User{
        private string $nickname;
        private int $old;
        private string $birth;
        private string $state;
    
        public function __construct($nickname,$old, $birth, $state) {
              $this->nickname = $nickname;
              $this->old = $old;
              $this->birth = $birth;
              $this->state = $state;
        }
        
        public function getOutput(){
            $output = "User{" . "\n";
            $output = $output ."nickname : " .$this->nickname. "\n";
            $output = $output ."old : " .$this->old. "\n";
            $output = $output ."birth : " .$this->birth. "\n";
            $output = $output ."state : " .$this->state. "\n";
            $output = $output ."}" . "\n";
            return $output;
        }
    }
    
    //main
    $N = trim(fgets(STDIN));
    for($i=0; $i<=$N; $i++){
        list($nickname, $old, $birth, $state) = explode(" ", str_replace(array("\r\n","\r","\n"), '', fgets(STDIN)));
        $member = new User($nickname, $old, $birth, $state);
        echo $member->getOutput();
    }
?>

アウトプットはうまくいっているんですがランタイムエラーが。。。

PHP Warning:  Undefined array key 1 in /workspace/Main.php on line 29
PHP Warning:  Undefined array key 2 in /workspace/Main.php on line 29
PHP Warning:  Undefined array key 3 in /workspace/Main.php on line 29
PHP Fatal error:  Uncaught TypeError: Cannot assign null to property User::$old of type int in /workspace/Main.php:10
Stack trace:
#0 /workspace/Main.php(30): User->__construct()
#1 {main}
  thrown in /workspace/Main.php on line 10

29行目というと、この部分ですね。

list($nickname, $old, $birth, $state) = explode(" ",trim(fgets(STDIN)));

list()を使わないとエラーが消えるので
list()に問題があるようです。
ただ、このlist()のエラーに関してまだ調べられていないので
原因が分かり次第こちらに上げます。
ひとまずlist()を使わない方法で解決。

<?php
    class User{
        private string $nickname;
        private int $old;
        private string $birth;
        private string $state;
    
        public function __construct($nickname,$old, $birth, $state) {
              $this->nickname = $nickname;
              $this->old = $old;
              $this->birth = $birth;
              $this->state = $state;
        }
        public function getOutput(){
            $output = "User{" . "\n";
            $output = $output ."nickname : " .$this->nickname. "\n";
            $output = $output ."old : " .$this->old. "\n";
            $output = $output ."birth : " .$this->birth. "\n";
            $output = $output ."state : " .$this->state. "\n";
            $output = $output ."}" . "\n";
            return $output;
        }
    }
    
    $num = trim(fgets(STDIN));
    
    for($i=0; $i<$num; $i++){
        $arr = explode(" ", str_replace(array("\r\n","\r","\n"), '', fgets(STDIN)));
        //下でもOKだが、下の方が簡単かな
        //$arr = explode(" ", trim(fgets(STDIN)));
        $member = new User($arr[0], $arr[1], $arr[2], $arr[3]);
        echo $member->getOutput();
    }
?>

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?