diff --git a/typo3/sysext/scheduler/Classes/Scheduler.php b/typo3/sysext/scheduler/Classes/Scheduler.php
index 5da5fb9b805981cda1bfe4e3ca7910dc207c9a41..0b03332fc62a2ad63234d9ba80616b92bf5bc219 100644
--- a/typo3/sysext/scheduler/Classes/Scheduler.php
+++ b/typo3/sysext/scheduler/Classes/Scheduler.php
@@ -355,13 +355,13 @@ class Scheduler implements SingletonInterface, LoggerAwareInterface
         }
         /** @var Task\AbstractTask $task */
         $task = unserialize($row['serialized_task_object']);
-        if ($task->getTaskGroup() === null) {
-            // Fix invalid task_group=NULL settings in order to avoid exceptions when saving on PostgreSQL
-            $task->setTaskGroup(0);
-        }
         if ($this->isValidTaskObject($task)) {
             // The task is valid, return it
             $task->setScheduler();
+            if ($task->getTaskGroup() === null) {
+                // Fix invalid task_group=NULL settings in order to avoid exceptions when saving on PostgreSQL
+                $task->setTaskGroup(0);
+            }
         } else {
             // Forcibly set the disable flag to 1 in the database,
             // so that the task does not come up again and again for execution