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

Solved Crash on call to a member funtion on null

Comments in 'Plugin Development' started by Tim // robske Büba, Dec 19, 2015.

  1. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    This code crashes (if Steve Joins), with
    "Fatal error: Call to a member function getSafeSpawn() on null in G:\Daten_Schnell\Tim\PocketServerTest\plugins\SignPortal-masterV2x2_1\src\leonchang99\SignPortal\Main.php on line 48"

    PHP:
      public function onPlayerJoinSteve(PlayerJoinEvent $e){
      
    $SteveIsOnline true;
      
    $playerJoinName $e->getPlayer()->getName();
      
    Server::getInstance()->broadcastMessage($playerJoinName);
      if (
    $playerJoinName == "Steve"){
      
    Server::getInstance()->broadcastMessage("SteveJoined");
      
    $mapname "SteveIsStupid";
      while(
    $SteveIsOnline == true){
      
    Server::getInstance()->broadcastMessage("IsStillOnline");
      
    $e->getPlayer()->teleport(Server::getInstance()->getLevelByName($mapname)->getSafeSpawn());
      
    sleep(5);
      
    $SteveIsOnline $e->getPlayer()->IsOnline();
      }
      }
      }
    Realy don't know why, because $e->getPlayer()->getName(); does return the name right?!
  2. Intyre
    Offline

    Intyre Staff Member PocketMine Team

    Joined:
    Aug 24, 2013
    Posts:
    118
    Minecraft User:
    Intyre
    Last edited: Dec 19, 2015
  3. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
  4. Intyre
    Offline

    Intyre Staff Member PocketMine Team

    Joined:
    Aug 24, 2013
    Posts:
    118
    Minecraft User:
    Intyre
  5. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    sleep() delays the running PHP process. That basically freezes the server for the amount of seconds, not just pausing your plugin.
    I also think getServer() is better practice than the static Server::getInstance().

    Also, try adding a Vector3 argument when calling getSafeSpawn(). The default argument is null.
    Last edited: Dec 20, 2015
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Not all processes. Just the PHP process. But that's bad enough, since everything in your server is in the PHP process.
    HotFireyDeath and Gamecrafter like this.
  7. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    Ok, I will remove the sleep and add a scheduled function
    And will replace Server::GetInstance with getServer()
    HotFireyDeath likes this.
  8. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    Ok, rewrote code but it doesn't even get to getSafeSpawn anymore (stuck on getServer) Is there an special use statement needed?
    Fatal error: Call to undefined function leonchang99\SignPortal\getServer() in ...\Main.php on line 45
    PHP:
      public function onPlayerJoinSteve(PlayerJoinEvent $e){
      
    $SteveIsOnline true;
      
    $playerJoinName $e->getPlayer()->getName();
      
    Server::getInstance()->broadcastMessage($playerJoinName);
      if (
    $playerJoinName == "Steve"){
      
    Server::getInstance()->broadcastMessage("SteveJoined");
      
    $mapname "SteveIsStupid";
      
    Server::getInstance()->broadcastMessage("IsStillOnline");
      
    $e->getPlayer()->teleport(getServer()->getLevelByName($mapname)->getSafeSpawn());
      
    $SteveIsOnline $e->getPlayer()->IsOnline();
      }
      }
    //TODO: Add scheduled function, when i got that level chnange to work...
  9. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    No one knows :(
  10. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    replace
    PHP:
    Server::getInstance()
    with
    PHP:
    $this->getServer()
    and
    PHP:
    $this->getServer()->getLevelByName();
    not
    PHP:
    getServer()->getLevelByName();
  11. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    Oh $this HOW COULD I FORGET ABOUT THAT Stupid me :( will try it again today
  12. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    PHP:
      public function onPlayerJoinSteve(PlayerJoinEvent $e){
      
    //$SteveIsOnline = true;
      
    $playerJoinName $e->getPlayer()->getName();
      
    $this->getServer()->broadcastMessage($playerJoinName);
      if (
    $playerJoinName == "Steve"){
      
    $this->getServer()->broadcastMessage("SteveJoined");
      
    $mapname "Lobby4";
      
    //$this->getServer()->broadcastMessage("IsStillOnline");
      
    $e->getPlayer()->teleport($this->getServer()->getLevelByName($mapname)->getSafeSpawn());
      
    //$SteveIsOnline = $e->getPlayer()->IsOnline();
      
    }
      }
    Will try this later, have to go to school :(
  13. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    The code mentioned above again calls the
    Fatal error: Call to a member function getSafeSpawn() on null in ServerCore\Main.php on line 45
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    This is how the #LearnPHP legend got started. Once upon a time someone called @PEMapModder learnt a bit Java from Android app programming, and got used to calling functions/methods without providing a context, and one of the (really) oldest members in the community called @wies yelled at me, learn PHP.

    Chaos - earliest people who contributed to PocketMine
    Uranus - the people who joined the community after the forums have been created (or after 1.3.0 Alpha was released) and before MCPE added the external server button (join external servers without mods)
    Titans - the people who joined after MCPE added that button
    Olympians - the people who joined or actively participated after MCPE 0.8.0 has been released, i.e. Alpha_1.3.x (except Steadfast) no longer support them.
    PocketKiller and Vaivez66 like this.
  15. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    Started my first server about 0.9.0
    But does anyone know how to fix that
    PHP:
      public function onPlayerJoinSteve(PlayerJoinEvent $e){
      
    //$SteveIsOnline = true;
      
    $playerJoinName $e->getPlayer()->getName();
      
    $this->getServer()->broadcastMessage($playerJoinName);
      if (
    $playerJoinName == "Steve"){
      
    $this->getServer()->broadcastMessage("SteveJoined");
      
    $mapname "Lobby4";
      
    //$this->getServer()->broadcastMessage("IsStillOnline");
      
    $e->getPlayer()->teleport($this->getServer()->getLevelByName($mapname)->getSafeSpawn());
      
    //$SteveIsOnline = $e->getPlayer()->IsOnline();
      
    }
      }

    code?
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What's wrong with that?
  17. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    That happens, when Steve joins!
    Fatal error: Call to a member function getSafeSpawn() on null in ...\ServerCore\Main.php on line 45
  18. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    Really, why do I manage to programs something, what does not work but should work and no one finds the mistake...
  19. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
    make sure $this->getServer()->getLevelByName($mapname) is instanceof Level...
  20. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    You mean if the world exists?
    It exists...

Share This Page

Advertisement