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

Solved Call to a member function getEffectLevel() on null

Discussion in 'Development' started by minijaham, Oct 30, 2020.

  1. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Code:
    Server thread/CRITICAL Error: "Call to a member function getEffectLevel() on null" (EXCEPTION) in "plugins/1MMORPG/src/minijaham/MMORPG/EventListener" at line 108
    30.10 17:10:32 [Server] Server thread/CRITICAL #0 src/pocketmine/plugin/MethodEventExecutor(42): minijaham\MMORPG\EventListener->shootBow(object pocketmine\event\entity\EntityShootBowEvent)
    30.10 17:10:32 [Server] Server thread/CRITICAL #1 src/pocketmine/plugin/RegisteredListener(80): pocketmine\plugin\MethodEventExecutor->execute(object minijaham\MMORPG\EventListener, object pocketmine\event\entity\EntityShootBowEvent)
    Full code:
    PHP:
    public function shootBow(EntityShootBowEvent $event) {
            
    $player $event->getEntity();
            
    $effect Effect::SLOWNESS;
            if (
    $player instanceof Player){
                if (
    $player->getEffect($effect)->getEffectLevel() === 0) {
                    
    $amount 8;
                    
    $anglesBetweenArrows = (45 / ($amount 1)) * M_PI 180;
                    
    $pitch = ($player->pitch 90) * M_PI 180;
                    
    $yaw = ($player->yaw 90 45 2) * M_PI 180;
                    
    /** @var Projectile $projectile */
                    
    $projectile $event->getProjectile();
                        for (
    $i 0$i $amount$i++) {
                        
    $class get_class($projectile);
                        
    $entityType substr($classstrrpos($class"\\") + 1);
                        
    $nbt Entity::createBaseNBT($player->add(0$player->getEyeHeight()), $player->getDirectionVector(), $player->yaw$player->pitch);
                        
    /** @var Projectile $newProjectile */
                        
    $newProjectile Entity::createEntity($entityType$player->getLevel(), $nbt$player$projectile);
                        if (
    $newProjectile instanceof Arrow$newProjectile->setPickupMode(Arrow::PICKUP_NONE);
                        
    $newProjectile->spawnToAll();
                        
    ProjectileTracker::addProjectile($newProjectile$item);

                        
    $newDirection = new Vector3(sin($pitch) * cos($yaw $anglesBetweenArrows $i), cos($pitch), sin($pitch) * sin($yaw $anglesBetweenArrows $i));
                        
    $newProjectile->setMotion($newDirection->normalize()->multiply($projectile->getMotion()->multiply($event->getForce())->length()));
                        if (
    $projectile->isOnFire()) $newProjectile->setOnFire($projectile->getFireTicks() / 20);
                    }
                    
    ProjectileTracker::removeProjectile($projectile);
                    
    $projectile->close();
                }
            }
        }
    Error line:
    PHP:
    if ($player->getEffect($effect)->getEffectLevel() === 0) {
     
    Primus and Dyntes like this.
  2. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    How do I solve this problem?
     
    Primus likes this.
  3. Dyntes

    Dyntes Spider Jockey

    Messages:
    48
    GitHub:
    Dyntes
    The error means that there are no getEffectLevel() function on the Effect class... The function is only callable on EffectInstance class... But I don't know how to get the EffectInstance of an effect... You can check the pocketmine source code for more information...
     
    Primus and minijaham like this.
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    $player->getEffect($effect) may return either EffectInstance (if the player has $effect) or null (if they don't have $effect).
    Assign it to a variable, check if it's null before proceeding further.
     
    Primus and minijaham like this.
  5. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Omg it's Muqsit :DDDDD hiii(also thank you for your reply :D)
     
    Primus 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.