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" (EXCEPTION) in "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); }
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.
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);}
I'm pretty sure $data[0] === "" isn't necessary since submitting the form without anything is $data === null as well
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\{CustomForm, SimpleForm, ModalForm};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); }
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 = nullif ($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}