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

Solved EnchantUI error

Discussion in 'Help' started by SomeServerOwner, May 25, 2021.

  1. SomeServerOwner

    SomeServerOwner Spider Jockey

    Messages:
    40
    [17:09:31] [Server thread/CRITICAL]: ErrorException: "Trying to access array offset on value of type null" (EXCEPTION) in "plugins/EnchantShopUI(6)(1)(1).phar/src/UnknownOre/EnchantUI/Main" at line 101


    Here is the code

    public function buyForm(Player $player,int $id): void{
    $array = $this->shop->getNested('shop');
    $form = new CustomForm(function (Player $player, $data = null) use ($array, $id){
    $var = array(
    "NAME" => $array[$id]['name'],
    "PRICE" => $array[$id]['price'] * $data[1],
    "LEVEL" => $data[1],
    "MONEY" => EconomyAPI::getInstance()->myMoney($player),
    "INCOMPATIBLE" => $incompatible = $this->isCompatible($player, $array[$id]['incompatible-enchantments'])
    );
    if ($data === null){
    $this->listForm($player);
    return;
    }
     
  2. Axon

    Axon Zombie

    Messages:
    276
    Line 101 is `$array[$id]['price']`
    I'm guessing it's null. Make sure the price is set
     
    Agent likes this.
  3. SomeServerOwner

    SomeServerOwner Spider Jockey

    Messages:
    40
    The prices are set, but whenever I do not buy anything in the form and I exit out of the form, I am being presented with the error.
     
  4. Axon

    Axon Zombie

    Messages:
    276
    You can check if the `$array` is null, if so then return;
    Eg:
    PHP:
    if($array == NULL){
        return;
    }
     
    Agent likes this.
  5. Axon

    Axon Zombie

    Messages:
    276
    Make sure the if function is in top of declaration of `$var`
     
    Agent likes this.
  6. DavyCraft648

    DavyCraft648 Spider Jockey

    Messages:
    40
    GitHub:
    DavyCraft648
    https://github.com/UnknownOre/EnchantShopUI/blob/master/src/UnknownOre/EnchantUI/Main.php#L101

    or the form $data is null

    swap this
    PHP:
    if ($data === null){
        
    $this->listForm($player);
        return;
    }
    $var = array(
        
    "NAME" => $array[$id]['name'],
        
    "PRICE" => $array[$id]['price'] * $data[1],
        
    "LEVEL" => $data[1],
        
    "MONEY" => EconomyAPI::getInstance()->myMoney($player),
        
    "INCOMPATIBLE" => $incompatible $this->isCompatible($player$array[$id]['incompatible-enchantments'])
    );
     
  7. SomeServerOwner

    SomeServerOwner Spider Jockey

    Messages:
    40
    It works now, thank you so much for your 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.