Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Call to member function on null

Comments in 'Plugin Development' started by RekkuzaRage, Jan 28, 2016.

  1. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    I been working on a CTF plugin for a while now, and I been testing this queue task that is fired when an arena is queued through a function. Players that join the queued match are added into a queue array. The task foreaches all the player usernames in the queue array and sends a popup to them. They are removed from the array once they leave the server or teleport to the lobby, however when they leave the server, the server crashes, most likely because the task runs before they are removed from the array and it results in the server crashing. I keep on getting call to member function onOnline() on null in crash dumps... Also checking if the foreached player is an instance of a Player doesn't work (I also think that would be a slow method...) Below is the code and I appreciate any help. Thanks!

    PHP:
    public function onRun($tick) {
             
    $queue $this->plugin->game[$this->arena]["queue"];
       
             if(
    $queue 1) {
                 return 
    false;
             }
             
             foreach(
    $queue as $p) {
                 
    $player $this->server->getPlayerExact($p);
                 if(
    $player->isOnline()) {
                     if(
    count($queue) < 2) {
                         
    $player->sendPopup("§8- §3Waiting for more players to start match.");
                     }
                     else {
                         
    $player->sendPopup("§8(§c" count($queue) . "§8/§c12§8)");
                     }
                 }
             }
         }
    Last edited: Jan 28, 2016
    thebigsmileXD likes this.
  2. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    653
    Plugins:
    1
    Minecraft User:
    jojoe77777
    What exactly happens if you check if $player is an instance of player?
  3. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    check if $player is an instanceof Player
  4. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    Tried that, it didn't work. It doesn't send the popup messages if I use I check if it is an instance of player.
  5. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    this means instanceof Player is working .
  6. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    Well why doesn't send the popup? It does when I use isOnline...
  7. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Same thing happens to me on SpleefPE.
  8. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Just try.. ->getPlayer($name) instead of *.Exact
    RekkuzaRage likes this.
  9. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Use is_null? isOnline is when the player instance actually exists.
  10. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    I got it to work. Thanks! Here is the updated code.
    PHP:
    public function onRun($tick) {
             
    $queue $this->plugin->game[$this->arena]["queue"];
           
             if(
    count($queue) < 1) {
                 return 
    false;
             }
                 
             foreach(
    $queue as $p) {
                 
    $player $this->server->getPlayer($p);
                 if(
    $player instanceof Player) {
                     if(
    count($queue) < 2) {
                         
    $player->sendPopup("§8- §3Waiting for more players to start match.");
                     }
                     else {
                         
    $player->sendPopup("§8(§c" count($queue) . "§8/§c12§8)");
                     }
                 }
             }
         }
    PEMapModder and Legoboy0215 like this.
  11. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    I see, thx, i'll search that error on my SpleefPE :D Welcome
  12. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    It's not an error, he simply was calling Player::isOnline() on a string which just happened to be the player name.
    thebigsmileXD likes this.
  13. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    I see, thanks!
  14. TryoneLegendZzz
    Offline

    TryoneLegendZzz Active Member

    Joined:
    May 31, 2015
    Posts:
    201
    Minecraft User:
    SavionLegendZzz
    i thought it was Player::isConnected() and why do you need Player::isOnline() ? when they quit it unset them so...?
  15. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    there is no isConnected.
  16. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
  17. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Oh i see. Theres the difference

    public function isOnline(){
    return $this->connected === true and $this->loggedIn === true;
    }

    public function isConnected(){
    return $this->connected === true;
    }

Share This Page

Advertisement