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

Call to a member function getRarity() on null

Discussion in 'Development' started by F0reaxh, Aug 27, 2019.

  1. F0reaxh

    F0reaxh Creeper

    Messages:
    5
    foreach(KitsManager::$kits as $id => $kit)
    {
    if($kit->getRarity() == $rarity)
    $i->addItem($kit->getItem()->setCustomName("§r§eКит ".$this->getColor($p,$kit).$kit->getName()." §eредкости §r".$kit->getStringRarity()));
    }
    Please help me!
     
  2. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    $kit is null, link to plugin?
     
  3. F0reaxh

    F0reaxh Creeper

    Messages:
    5
    no links, this is my plugin
     
  4. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Then at least post the full code?
    If you want to fix it yourself, the problem is something is causing KitsManager::$kits to have null
     
  5. F0reaxh

    F0reaxh Creeper

    Messages:
    5
    <?php

    namespace Cust0mPhase\Managers;

    use pocketmine\item\{Item as I,enchantment\Enchantment as E};

    class KitsManager{

    /**
    * deleted kits: armorsmith, cannoneer, enchanter, fisherman, pharaoh, snowman, disco, grenade, princess, batguy
    * deleted perks: ender mastery, instant smelting, marksmanship, mining expertise, knowledge, annoy-o-mite, revenge
    */

    const ARMORER = 0;
    const BASEBALL_PLAYER = 1;
    const ECOLOGIST = 2;
    const ENDERCHEST = 3;
    const ENDERMAN = 4;
    const FARMER = 5;
    const HUNTER = 6;
    const KNIGHT = 7;
    const PYRO = 8;
    const ROOKIE = 9;
    const SPELEOLOGIST = 10;
    const TROLL = 11;
    const FROG = 12;
    const SCOUT = 13;
    const CANNONEER = 14;

    //const BATGUY = 12;
    //const ENERGIX = 12;

    /** @var \SplFixedArray */
    public static $kits = null;

    /**
    * @param Item $item
    *
    * @return mixed
    */
    public static function getKitByItem(I $item){
    foreach(self::$kits as $kId => $kit){
    if($kit->getItem()->getId() == $item->getId()) return $kit;
    }
    return null;
    }

    /**
    * @param array $args
    * @param int $id
    * @param int $lvl
    *
    * @return Item
    */
    public static function enchant(array $args,int $id,int $lvl){
    $item = I::get($args[0],isset($args[1])?$args[1]:0,isset($args[2])?$args[2]:1);
    return $item->addEnchantment(E::getEnchantment($id)->setLevel($lvl));
    }

    /**
    * @param array $args
    * @param int $id
    * @param int $lvl
    * @param int $id1
    * @param int $lvl1
    * @param int $id2
    * @param int $lvl2
    *
    * @return Item
    */
    public static function massiveEnchant(array $args,int $id,int $lvl,int $id1,int $lvl1,int $id2 = null,int $lvl2 = null){
    $item = I::get($args[0],isset($args[1])?$args[1]:0,isset($args[2])?$args[2]:1);
    if($id2 == null)
    return $item->addEnchantment(E::getEnchantment($id)->setLevel($lvl))->addEnchantment(E::getEnchantment($id1)->setLevel($lvl1));
    else{
    $i = $item->addEnchantment(E::getEnchantment($id)->setLevel($lvl))->addEnchantment(E::getEnchantment($id1)->setLevel($lvl1));
    return $i->addEnchantment(E::getEnchantment($id2)->setLevel($lvl2));
    }
    }

    /**
    * @param $args
    * @param string $customName
    *
    * @return Item
    */
    public static function setCName(array $args,string $customName){
    $item = I::get($args[0],isset($args[1])?$args[1]:0,isset($args[2])?$args[2]:1);
    return $item->setCustomName($customName);
    }

    /*Armorer - 2 => [I::get(307),I::get(316),I::get(317)],3 => [self::enchant([307],0,1),I::get(316),I::get(309)]
    * Baseball Player - 2 => [self::enchant([306],0,1),I::get(308),self::enchant([268],12,1)],3 => [self::enchant([310],0,1),I::get(308),self::enchant([267],12,1)]
    * Ecologist - 2 => [self::enchant([258],15,2),I::get(17,0,16),I::get(316)],3 => [self::enchant([279],15,1),I::get(17,0,32),I::get(316)]
    * Enderchest - 2 => [I::get(322,0,2),I::get(D_BOOTS)],3 => [I::get(322,0,4),$this->enchant([D_BOOTS,0,1),I::get(I_CHESTPLATE)]
    * Enderman - 2 => [I::get(368,0,2),I::get(I_SWORD)],3 => [I::get(368,0,3),I::get(I_SWORD),I::get(I_LEGGINGS)]
    */

    /**
    * @param $id
    * @param $name
    * @param $items
    * @param $description
    * @param $rarity
    * @param $item

    * @return bool
    */
    public static function registerKit($id,$name,$items,$description,$rarity,$item){
    if(isset(self::$kits[$id])) return false;
    self::$kits[$id] = new KitsManager($name,$items,$description,$rarity,$item);
    return true;
    }

    /**
    * @return KitsManager[]
    */
    public static function getKits(){
    return self::$kits;
    }

    /**
    * @return mixed
    */
    public static function getKit(int $id){
    if(!isset(self::$kits[$id])) return false;
    return self::$kits[$id];
    }

    /**
    * @return mixed
    */
    public static function getKitByName(string $name){
    if(!defined(KitsManager::class."::".strtoupper($name))) return false;
    return self::getKit(constant(KitsManager::class."::".strtoupper($name)));
    }

    private $name;
    private $items;
    private $description;
    private $rarity;
    private static $instance = null;

    /**
    *
    * KitsManager constructor.
    *
    * @param $id
    * @param $name
    * @param $items
    * @param $description
    * @param $rarity
    * @param $item
    */
    public function __construct($name,$items,$description,$rarity,$item){
    $this->name = (string) $name;
    $this->items = $items;
    $this->description = (string) $description;
    $this->rarity = (string) $rarity;
    $this->item = (int) $item;
    self::$instance = $this;
    }

    public static function getInstance(){
    return self::$instance;
    }

    /**
    * @return string
    */
    public function getName(){
    if($this->name == "Baseball_Player") return "Baseball Player";
    return $this->name;
    }

    /**
    * @return array
    */
    public function getItems(){
    return $this->items;
    }

    /**
    * @return string
    */
    public function getDescription(){
    return $this->description;
    }

    /**
    * @return string
    */
    public function getRarity(){
    return $this->rarity;
    }

    /**
    * @return Item
    */
    public function getItem(){
    $item = I::get($this->item);
    //if($this->getName() == "Frog") $item->setCustomColor(\pocketmine\utils\Color::getDyeColor(10));
    return $item;
    }

    /**
    * @return int
    */
    public function getPrice(){
    $price = 4000;
    switch($this->getRarity()){
    case "rare": $price = 6000; break;
    case "legendary": $price = 8000; break;
    }
    return $price;
    }

    /**
    * @param $level
    *
    * @return int
    */
    public function getLevelPrice(int $level){
    $price = 2000;
    switch($this->getRarity()){
    case "common":
    if($level == 2) $price = 2000;
    if($level == 3) $price = 2500;
    break;
    case "rare":
    if($level == 2) $price = 4000;
    if($level == 3) $price = 4500;
    break;
    case "legendary":
    if($level == 2) $price = 6000;
    if($level == 3) $price = 6500;
    break;
    }
    return $price;
    }

    /**
    * @return string
    */
    public function getLowName(){
    $n = strtolower($this->getName());
    return str_replace(" ","_",$n);
    }

    /**
    * @return string
    */
    public function getStringRarity(){
    $str = "§a§lCOMMON§r";
    switch($this->getRarity()){
    case "rare": $str = "§9§lRARE§r"; break;
    case "legendary": $str = "§6§lLEGENDARY§r"; break;
    }
    return $str;
    }
    }
     
    Last edited: Aug 28, 2019
  6. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    You have called

    PHP:
    null->getRarity();
    but null hasn't this function... so simply look at the line that's showed in the error and change null to some object that has getRarity() function, or remove the line
     
    Muqsit likes this.
  7. F0reaxh

    F0reaxh Creeper

    Messages:
    5
    if I delete this line, there will be no sorting of whales by rarity
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Well then don't put null values in KitsManager::$kits
     
  9. F0reaxh

    F0reaxh Creeper

    Messages:
    5
    They should not be zero, since whales are added to the array of the plugin
     
  10. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    As Wolfdale said, you'll need to post the full code to get help.
     
  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.