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

"Call to a member function getLevel() on null"

Discussion in 'Development' started by KevY, Jun 21, 2017.

  1. KevY

    KevY Creeper

    Messages:
    3
    I'm working on a plugin, and I wanted to compare a level(world) with another one matching from config file, Now there is a player I have whose world/level I want to compare but whenever I try that I get this error and I also have the same problem with checking for permissions, I really dont know why this occurs and this is making me crazy I'm tired of looking for a solution.
    Here is my code, there is a ! at the end of the lines with the error of this null thingy.
    Code:
    
    $perm = $this->plugin->getServer()->getPlayer($player)->hasPermission("beaconsplus.effect"); !
    if($perm == false)
    {
          continue;
    }
    if($this->plugin->getServer()->getLevelByName($beaconLevel) == $this->plugin->getServer()->getPlayer($player)->getLevel()) !
     {
    
    Please help me, I'm new to php though.
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    What is $player? getPlayer($player) is returning null instead of a Player object, which means getLevel was being called on null, thus the error.
     
  3. KevY

    KevY Creeper

    Messages:
    3
    this is the code before that
    Code:
    foreach($this->plugin->getServer()->getOnlinePlayers() as $player)
                         {
                             /*$perm = $this->plugin->getServer()->getPlayer($player)->hasPermission("beaconsplus.effect");
                             if($perm == false) 
                             {
                                 continue;
                             }*/
                             if($this->plugin->getServer()->getLevelByName($beaconLevel) == $this->plugin->getServer()->getPlayer($player)->getLevel())
                             {
                                  if($this->plugin->getServer()->getPlayer($player)->distance($beaconPos) <= $this->plugin->getConfig()->get("beacon-range"))
                                  {
    
    what do u think is wrong with $player? is it suppoed to return the player
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Why do you get the player of a player :facepalm:
    $server->getOnlinePlayers() already returns an array of players, not an array of player names.
     
  5. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    After you foreach the online players, you don't need to use Server->getPlayer, because you already have the Player object to use.
     
  6. KevY

    KevY Creeper

    Messages:
    3
    damn.. I completely missed that out, I thought that returned a player name xD my bad.
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Mark the thread solved if your issue was solved
     
    Teamblocket likes 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.