diff --git a/core/Command/User/Setting.php b/core/Command/User/Setting.php
index e2e65f7d5f98d..16e851d82523f 100644
--- a/core/Command/User/Setting.php
+++ b/core/Command/User/Setting.php
@@ -219,7 +219,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
 		}
 	}
 
-	protected function getUserSettings($uid, $app) {
+	protected function getUserSettings(string $uid, string $app): array {
 		$settings = $this->config->getAllUserValues($uid);
 		if ($app !== '') {
 			if (isset($settings[$app])) {
@@ -230,7 +230,10 @@ protected function getUserSettings($uid, $app) {
 		}
 
 		$user = $this->userManager->get($uid);
-		$settings['settings']['display_name'] = $user->getDisplayName();
+		if ($user !== null) {
+			// Only add the display name if the user exists
+			$settings['settings']['display_name'] = $user->getDisplayName();
+		}
 
 		return $settings;
 	}