Programmatically reindexing by product ids results in Uncaught TypeError

I’ve got a custom module for importing/creating products programmatically. After products are imported I would like to reindex only the new product ids. See code below.

private function _reindexByProductIds($ ids, $ indexers) { foreach($ indexers as $ indexer) { $ index = $ this->_indexerRegistry->get($ indexer); if(!$ index->isScheduled()) { $ index->reindexList($ ids); } } }

$ ids is an array of imported ids and $ indexers is an array of all the indexer ids. This function results in an Uncaught TypeError when trying to reindex ‘catalog_product_flat’, but when I run the indexer:reindex command I don’t get this error. See error below.

Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Catalog\Model\Indexer\Product\Flat\TableBuilder::_fillTemporaryEntityTable() must be of the type array, null given, called in /Users/robertbisschop/Documents/Development/ on line 93 and defined in /Users/robertbisschop/Documents/Development/ Stack trace: #0 /Users/robertbisschop/Documents/Development/ Magento\Catalog\Model\Indexer\Product\Flat\TableBuilder->_fillTemporaryEntityTable(‘catalog_product…’, NULL, Array) #1 /Users/robertbisschop/Documents/Development/ Magento\Catalog\Model\Indexer\Product\Flat\TableBuilder->build(‘1’, A in /Users/robertbisschop/Documents/Development/ on line 219

I’ve been debugging for a while, but can’t figure out how to solve it. Any help is highly appreciated.