Error: CreateWidget called with a null class.

I’ve tried everything possible to fix this bug, including making CreateWidget synchronous by setting the asset subclass of UKeybindingUMG in MyPlayerController, and setting the player controller, UMG and Game Mode instances in the Unreal Editor. I’ve also tried changing BeginPlay() to PostInitializeComponents() in MyPlayerController.

Here’s my code:

MyPlayerController.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "GameFramework/GameMode.h"
#include "MyPlayerController.generated.h"

UCLASS()
class MY_API AMyPlayerController : public APlayerController
{
	GENERATED_BODY()

public:

	virtual void PostInitializeComponents() override;

	//The class that will be used for the keybinding UI
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Player, HUD and UI")
		TAssetSubclassOf<class UKeybindingUMG> KeybindingUIClass;

	//The instance of the KeybindingUMG UI
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Player, HUD and UI")
		class UKeybindingUMG* KeybindingUMG;
	
};

MyPlayerController.cpp:

// Fill out your copyright notice in the Description page of Project Settings.

#include "MyPlayerController.h"
#include "My.h"
#include "KeybindingUMG.h"

void AMyPlayerController::PostInitializeComponents()
{
	Super::PostInitializeComponents();

	if (IsLocalPlayerController())
	{
	    if (!KeybindingUMG) //if KeybindingUMG isn't created & null
	    {
  KeybindingUMG = CreateWidget<UKeybindingUMG>(this, KeybindingUIClass.LoadSynchronous());
				if (!KeybindingUMG)
					return;
	    KeybindingUMG->AddToViewport(); 
	    }

	}
}

KeybindingUMG.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "KeybindingUMG.generated.h"

UCLASS()
class MY_API UKeybindingUMG: public UUserWidget
{
	GENERATED_BODY()

};

KeybindingUMG.cpp:

// Fill out your copyright notice in the Description page of Project Settings.

#include "KeybindingUMG.h"
#include "My.h"

But nonetheless, these attempts to make CreateWidget still isn’t making CreateWidget detect the KeybindingUMG, and thus returning “CreateWidget called with a null class” in Unreal Editor Output Log.

Are there any other solutions to this problem?

Update #1: I’ve tried the code below in MyPlayerController.cpp, while just using UUserWidget instead of KeybindingUMG:

if (KeybindingUIClass = MyWidgetClassRef.TryLoadClass<UUserWidget>()) {
					KeybindingUMG = CreateWidget<UUserWidget>(this, KeybindingUIClass);
					if (!KeybindingUMG)
					return;

					KeybindingUMG->AddToViewport(); 
				}

However, it still doesn’t work. CreateWidget cannot be called, which means TryLoadClass is returning null.

Yes, I’ve found the answer by myself after 2 days of intense debugging.

At first, I changed to using GENERATED_UCLASS_BODY() in MyPlayerController.h

Then, in MyPlayerController.cpp, I assigned KeybindingUIClass in the constructor instead:

AMyPlayerController::AMyPlayerController(const FObjectInitializer& ObjectInitializer)
	:Super(ObjectInitializer)
{
	ConstructorHelpers::FClassFinder<UUserWidget> MyWidgetClassRef(TEXT("/Game/Maps/Dev/KeybindingWidget"));
	if (MyWidgetClassRef.Class) {
		KeybindingUIClass = MyWidgetClassRef.Class;
	}
}

And bug solved!!!

1 Like

thank you