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

Trying to acess array offset on null

Discussion in 'Plugin Help' started by WEATHERCRAFTYT1, Feb 27, 2021.

  1. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
    Can anyone fix this error. This error is in a Customform. When they click they x button on the form they get this. I been trying to fix it but I cant find error.

    PHP:
    [19:58:40] [Server thread/CRITICAL]: ErrorException"Trying to access array offset on value of type null" (EXCEPTIONin "plugins/UHC HARDCORE-master/src/Uhcr/sttings/Sttings" at line 209

    [19:58:40] [Server thread/CRITICAL]: #0 plugins/UHC HARDCORE-master/src/Uhcr/sttings/Sttings(209): pocketmine\utils\Utils::errorExceptionHandler(integer 8, string[51] Trying to access array offset on value of type null, string[72] /home/container/plugins/UHC HARDCORE-master/src/Uhcr/sttings/Sttings.php, integer 209, array[2])

    [19:58:40] [Server thread/CRITICAL]: #1 plugins/UHC HARDCORE-master/src/Uhcr/form/Form(91): Uhcr\Uhcr->Uhcr\sttings\{closure}(object pocketmine\Player, NULL )

    [19:58:40] [Server thread/CRITICAL]: #2 pmsrc/src/pocketmine/Player(3648): Uhcr\form\Form->handleResponse(object pocketmine\Player, NULL )

    [19:58:40] [Server thread/CRITICAL]: #3 pmsrc/src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter(259): pocketmine\Player->onFormSubmit(integer 13, NULL )

    [19:58:40] [Server thread/CRITICAL]: #4 pmsrc/src/pocketmine/network/mcpe/protocol/ModalFormResponsePacket(49): pocketmine\network\mcpe\PlayerNetworkSessionAdapter->handleModalFormResponse(object pocketmine\network\mcpe\protocol\ModalFormResponsePacket)

    [19:58:40] [Server thread/CRITICAL]: #5 pmsrc/src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter(109): pocketmine\network\mcpe\protocol\ModalFormResponsePacket->handle(object pocketmine\network\mcpe\PlayerNetworkSessionAdapter)

    [19:58:40] [Server thread/CRITICAL]: #6 pmsrc/src/pocketmine/network/mcpe/protocol/BatchPacket(130): pocketmine\network\mcpe\PlayerNetworkSessionAdapter->handleDataPacket(object pocketmine\network\mcpe\protocol\ModalFormResponsePacket)

    [19:58:40] [Server thread/CRITICAL]: #7 pmsrc/src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter(109): pocketmine\network\mcpe\protocol\BatchPacket->handle(object pocketmine\network\mcpe\PlayerNetworkSessionAdapter)

    [19:58:40] [Server thread/CRITICAL]: #8 pmsrc/src/pocketmine/Player(3295): pocketmine\network\mcpe\PlayerNetworkSessionAdapter->handleDataPacket(object pocketmine\network\mcpe\protocol\BatchPacket)

    [19:58:40] [Server thread/CRITICAL]: #9 pmsrc/src/pocketmine/network/mcpe/RakLibInterface(169): pocketmine\Player->handleDataPacket(object pocketmine\network\mcpe\protocol\BatchPacket)

    [19:58:40] [Server thread/CRITICAL]: #10 pmsrc/vendor/pocketmine/raklib/src/server/ServerHandler(95): pocketmine\network\mcpe\RakLibInterface->handleEncapsulated(string[18] 65.99.97.104 62664, object raklib\protocol\EncapsulatedPacket, integer 0)

    [19:58:40] [Server thread/CRITICAL]: #11 pmsrc/src/pocketmine/network/mcpe/RakLibInterface(109): raklib\server\ServerHandler->handlePacket()

    [19:58:40] [Server thread/CRITICAL]: #12 pmsrc/src/pocketmine/network/mcpe/RakLibInterface(99): pocketmine\network\mcpe\RakLibInterface->process()

    [19:58:40] [Server thread/CRITICAL]: #13 pmsrc/vendor/pocketmine/snooze/src/SleeperHandler(113): pocketmine\network\mcpe\RakLibInterface->pocketmine\network\mcpe\{closure}()

    [19:58:40] [Server thread/CRITICAL]: #14 pmsrc/vendor/pocketmine/snooze/src/SleeperHandler(75): pocketmine\snooze\SleeperHandler->processNotifications()

    [19:58:40] [Server thread/CRITICAL]: #15 pmsrc/src/pocketmine/Server(2156): pocketmine\snooze\SleeperHandler->sleepUntil(double 1614455920.3254)

    [19:58:40] [Server thread/CRITICAL]: #16 pmsrc/src/pocketmine/Server(1993): pocketmine\Server->tickProcessor()

    [19:58:40] [Server thread/CRITICAL]: #17 pmsrc/src/pocketmine/Server(1587): pocketmine\Server->start()

    [19:58:40] [Server thread/CRITICAL]: #18 pmsrc/src/pocketmine/PocketMine(291): pocketmine\Server->__construct(object BaseClassLoader, object pocketmine\utils\MainLogger, string[16] /home/container/, string[24] /home/container/plugins/)

    [19:58:40] [Server thread/CRITICAL]: #19 pmsrc/src/pocketmine/PocketMine(321): pocketmine\server()

    [19:58:40] [Server thread/CRITICAL]: #20 pmsrc(11): require(string[71] phar:///home/container/PocketMine-MP.phar/src/pocketmine/PocketMine.php)
    PHP:
    public function inputStting(Player $player) : void {

                                 
    $form $this->createCustomForm(function(Player $player,array $data null){

                                               
    $result $data[0];

                                               if(
    $result === null){

                                                              return 
    true;

                                               }

                                               
    $code $result[0] ?? false;

                                               if(
    $code==false) {

                                                              
    $player->sendMessage("§c[ERROR]§6 You never put a clan name");

                                                              } else if(
    $this->clanRunning($result)==true) {

                                                                            
    $this->addPlayerClan($player,$result);

                   } else {

                                 
    $player->sendMessage("§c[ERROR]§6 This clan doesn't exist!");

                                 }

               });

                                 
    $form->setTitle(TE::BOLD "§fINPUT CLAN");

                                 
    $form->addInput("here put the name!");

                                 
    $form->sendToPlayer($player);

                                 }
     
  2. Primus

    Primus Zombie Pigman

    Messages:
    749
    Whenever user closes the form, that is submits no data, the passed value $data is null. You can not access element in offset 0 on non-array.

    Funny is how you have the guard check in place to prevent this error, but it's in the wrong order. Move "$result = $data[0];" below the if statement.
     
  3. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
    If I do that the $result will be undefined.
     
  4. DavyCraft648

    DavyCraft648 Spider Jockey

    Messages:
    40
    GitHub:
    DavyCraft648
    Try this
    PHP:
    public function inputStting(Player $player) : void {
       
    $form $this->createCustomForm(function(Player $player, array $data null) {
          if (
    $data === null // player clicks the x button
             
    or $data[0] === "" // player submit an empty text
          
    ) {
             
    $player->sendMessage("§c[ERROR]§6 You never put a clan name");
             return;
          }
          
    $result $data[0];
          if (
    $this->clanRunning($result) == true) {
             
    $this->addPlayerClan($player$result);
          } else {
             
    $player->sendMessage("§c[ERROR]§6 This clan doesn't exist!");
          }
       });
       
    $form->setTitle(TE::BOLD "§fINPUT CLAN");
       
    $form->addInput("here put the name!");
       
    $player->sendForm($form); // the same as $form->sendToPlayer($player);
    }
     
    Last edited: Mar 2, 2021
  5. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    I'm pretty sure $data[0] === "" isn't necessary since submitting the form without anything is $data === null as well
     
  6. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    This problem can be solved using something like this instead:
    PHP:
    public function BruhMomentumGoBrrr($player) {
            
    $form = new CustomForm(function (Player $player$data){
                    if (
    $data[1] == null){
                        return 
    true;
                    }
    This directly checks for the specified data returned.

    Here's a code I wrote a while ago for my Prisons server.

    PHP:
    use jojoe77777\FormAPI\{CustomFormSimpleFormModalForm};
    use 
    onebone\economyapi\EconomyAPI;

    public function 
    Convert($player) {
            
    $form = new CustomForm(function (Player $player$data){
                    if (
    $data[1] == null){
                        return 
    $this->BlackMarket($player);
                    }
                    
    $name $player->getName();
                    
    $money EconomyAPI::getInstance()->myMoney($player);
                    
    $cost = (int)$data[1]*1000;
                    if (!(int)
    $data[1]){
                        
    $player->sendMessage("The amount must be a number!");
                    }
                    elseif(
    $money >= $cost){
                        
    $allTokens $this->db->get($name);
                        
    $this->db->set($player->getName(), $allTokens $data[1]);
                        
    EconomyAPI::getInstance()->reduceMoney($player$cost);
                        
    $player->sendMessage("§8§lBlackMarket Merchant §7>>§r §aThanks for the trade...");
                    } else 
    $player->sendMessage("§8§lBlackMarket Merchant §7>>§r §cBring me more money...");
                });
                
    $name $player->getName();
                
    $allTokens $this->db->get($name);
                
    $form->setTitle("§7-=§o§l§8BlackMarket§r§7=-");
                
    $form->addLabel("§71 token = $1,000\n§bYour Money: §9".EconomyAPI::getInstance()->myMoney($player)."\n§6Tokens: §a".$allTokens);
                
    $form->addInput("Amount of tokens");
                
    $form->sendToPlayer($player);
        }
     
  7. DavyCraft648

    DavyCraft648 Spider Jockey

    Messages:
    40
    GitHub:
    DavyCraft648
    tried var_dump($data)?
    when player submitting the form, it will be an array instead of null
     
    Last edited: Mar 3, 2021
  8. DavyCraft648

    DavyCraft648 Spider Jockey

    Messages:
    40
    GitHub:
    DavyCraft648
    PHP:
    $data = [""];
    if (
    $data === null) {
         echo 
    "data is null".PHP_EOL// false
    }
    if (
    $data[0] === "")  { // the same as  $data[0] == null
        
    echo "first data is empty".PHP_EOL// true
    }
    if (
    $data[0] === null) {
        echo 
    "first data is null".PHP_EOL// false
    }
    PHP:
    $data null
    if ($data === null) {
         echo 
    "data is null".PHP_EOL// true
    }
    if (
    $data[0] === "")  { // error
        
    echo "first data is empty".PHP_EOL// false
    }
    if (
    $data[0] === null) { // error
        
    echo "first data is null".PHP_EOL// false
    }
     
    Last edited: Mar 3, 2021
    Primus and minijaham like this.
  9. DavyCraft648

    DavyCraft648 Spider Jockey

    Messages:
    40
    GitHub:
    DavyCraft648
     
    Last edited: Mar 3, 2021
    Primus likes this.
  10. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    You're right. I haven't read the error until now ;
     
    Primus likes this.
  11. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
    Lemme try this. Thanks
     
  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.