1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved Undefind index error!

Discussion in 'Development' started by KHAV, Jul 3, 2018.

  1. KHAV

    KHAV Baby Zombie

    Messages:
    152
    GitHub:
    xkhhv
    My code is working but why i have this error when the code run:
    Code:
    [20:10:19] [Server thread/CRITICAL]: Could not pass event 'pocketmine\event\player\PlayerChatEvent' to 'KICKMS v1.0.0': Undefined index: on KICKMS\Main
    [20:10:19] [Server thread/CRITICAL]: ErrorException: "Undefined index:" (EXCEPTION) in "KICKMS/src/KICKMS/Main" at line 772
    This is 772 line:
    PHP:
    if($this->games[$this->getGameByPlayer($player)]["Status"] == "INGAME"){
     
  2. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    we need more lol what is $this->games[],$this->getGameByPlayer
     
  3. KHAV

    KHAV Baby Zombie

    Messages:
    152
    GitHub:
    xkhhv
    This is $this->games:
    PHP:
    public $games = ["Game1" => ["Arena" => "RM-1""Status" => "JOINABLE"]];
    And this is getGameByPlayer function:
    PHP:
    public function getGameByPlayer($player){
    if(
    is_null($player)) return;
    foreach(
    $this->games as $game => $value){
    if(
    $value["Arena"] == $player->getLevel()->getFolderName()){
    return 
    $game;
    }}}
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    $this->games[$this->getGameByPlayer($player)]
    Are you use that entry exists in $games? Note that game can be null/void in your getGameByPlayer() method, which equates to "" key.

    The error happens when you try accessing an index of an empty.
    PHP:
    $x = [];
    $x["INDEX"] === "SOMETHING";//Undefined index "INDEX"

    $x["INDEX"] = [];
    $x["INDEX"]["INDEX2"] === "SOMETHING";//Undefined index "INDEX2"
     
    jasonwynn10 and KHAV like this.
  5. KHAV

    KHAV Baby Zombie

    Messages:
    152
    GitHub:
    xkhhv
    Thank you i made this function:
    PHP:
    public function inGame($player){
    if(
    $this->getGameByPlayer($player) == ""){
    return 
    false;
    }else{
    return 
    true;
    }}
    and add it up of:
    PHP:
    if($this->games[$this->getGameByPlayer($player)]["Status"] == "INGAME"){
    and became like this:
    PHP:
    if($this->inGame($player){
    if(
    $this->games[$this->getGameByPlayer($player)]["Status"] == "INGAME"){
    //do something
    }}
    And it works, Solved!
     
    jasonwynn10 and Muqsit like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.