User error: "0" is an invalid render array key in Drupal\Core\Render\Element::children() I receive this error message when I try to fetch data from the Database Table of a custom form and print it in a table format in Drupal 9.
User error: &quot;0&quot; is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core\lib\Drupal\Core\Render\Element.php).
Drupal\Core\Render\Element::children(Array) (Line: 349)
Drupal\Core\Render\Element\Table::preRenderTable(Array)
This is my code controller.php File code:
&lt;?php
    namespace Drupal\ad_form\Controller;
    use Drupal\Core\Controller\ControllerBase;
    use Drupal\Core\Database\Database;

    class ad_formController extends ControllerBase{

        public function getAdList(){

            $query = \Drupal::database();
            $result = $query-&gt;select('ad_form_submissions','e')
                    -&gt;fields('e',['id','name','gender','product','quantity','price'])
                    -&gt;execute()-&gt;fetchAll(\PDO::FETCH_OBJ);

            $data = [];

            foreach($result as $row){
                $data = [
                    'id'=&gt; $row-&gt;id,
                    'name'=&gt; $row-&gt;name,
                    'crop_category'=&gt; $row-&gt;gender,
                    'crop_type'=&gt; $row-&gt;product,
                    'quantity'=&gt; $row-&gt;quantity,
                    'price'=&gt; $row-&gt;price
                ];
            }

            $header = array('ID','Name','Gender', 'Product', 'Quantity', 'Price');

            $build['table'] = [
                '#type'=&gt;'table',
                '$header'=&gt;$header,
                '#row'=&gt;$data
            ];

            return [
                $build,
                '#title'=&gt; 'Submit Ad List'
            ];
        
        }

    }

How to resolve this issue and fetch the Data Correctly? I'm a Beginner, So please try to simplify the answer.
You should return a render array $build. if you want to add a title to the page add title in render array $build.
You can use $build['title'] = [ '#markup' =&gt; 'Yours title goes here ];
and return the $build
and in table element use #header instead of $header