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 setItem() on null"

Discussion in 'Development' started by David Flash, Aug 20, 2020.

  1. David Flash

    David Flash Spider Jockey

    Messages:
    26
    GitHub:
    davidflashcz
    Hi, I am making a server core for a while now and everything works, but there is one problem and that is sometimes when player is teleported to another world it throws this error and I don't know why. (The main thing is that it errors only sometimes) Event used: EntityLevelChangeEvent

    Code:
    PHP:
    if($target->getName() === "Lobby"){
                    
    $player->setGamemode(2);
                    
    $player->getInventory()->clearAll();
                    
    $player->getArmorInventory()->clearAll();
                    
    $this->getScheduler()->scheduleDelayedTask(new class($this$event->getEntity()) extends Task{
                        protected 
    $main;
                        public 
    $player;

                        public function 
    __construct(Main $mainCommandSender $player){
                            
    $this->main $main;
                            
    $this->player $player;

                        }

                        public function 
    onRun(int $currentTick){

                                
    $iron ItemFactory::get(26701);
                                
    $iron->setCustomName("§l§6Minigames§r§7\n[Use]");

                                
    $show ItemFactory::get(351101);
                                
    $show->setCustomName("§l§7Players §aVisible§r§7\n[Use]");

                                
    $lang ItemFactory::get(34501);
                                
    $lang->setCustomName("§l§7Settings§r§7\n[Use]");

                                
    $gadgets ItemFactory::get(46701);
                                
    $gadgets->setCustomName("§l§9Gadgets§r§7\n[Use]");

                            
    $this->player->getInventory()->setItem(0$iron);
                            
    $this->player->getInventory()->setItem(4$gadgets);
                            
    $this->player->getInventory()->setItem(7$show);
                            
    $this->player->getInventory()->setItem(8$lang);
                            }

                    },
                    
    5
                    
    );
                  }
     
  2. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    What error?
     
  3. David Flash

    David Flash Spider Jockey

    Messages:
    26
    GitHub:
    davidflashcz
    The error is in the title:
    "Call to a member function setItem() on null"
     
  4. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    what do you mean? how are you using $player without obj in onRun function????? what do you want to do?
    no explain = no help


    and
    wtf? if you see 1 sec onRun fuinction you can see he used $player without player obj, why do you asking this question??:eek::facepalm::facepalm::facepalm:
     
    Last edited: Aug 20, 2020
  5. David Flash

    David Flash Spider Jockey

    Messages:
    26
    GitHub:
    davidflashcz
    I know, it would be normal if it didn't work anytime but it works but sometimes it throws this error, I tried to add player object to the onRun function too, but it did the same thing. I don't know how to explain it properly. It works, the plugin works but sometimes like 2 times a day it throws this error for some reason..
     
  6. David Flash

    David Flash Spider Jockey

    Messages:
    26
    GitHub:
    davidflashcz
    Nvm, I think I fixed it. Thanks for your time :D
     
  7. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    you can ger players in that func like:
    PHP:
    foreach(Server::getinstance()->getOnlinePlayers as $player) {
    //code here
    }
    i think you used another files for onRun
    and for that i used Server::getinstance()
     
  8. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    Without the whole code neither you, neither me cannot 100% say that there is a mistake.

    Also, please do not send code which doesn't work and you didn't test it. I think you need more experience to help people on pmmp forums

    for example here
    upload_2020-8-20_18-58-59.png

    you have specified Server::getInstance() method wrong and also Server->getOnlinePlayers() is function, NO property as you have said.
     
  9. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    i didnt get your target but i said, open your eyes, i said if you created task in another files you should use Server::getInatnce() instead $this->getServer(), you should open you eyes befor POSTING.
     
  10. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    also
    PHP:
    foreach($this->getServer()->getOnlinePlayers as $player) {}
    working fine, befor posting upgrade your knowledge bruh LMAO
     
  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.