CreateWidget called with a null class Error

Hi, everybody. I have a UUserWidget class InventoryWidget

I need placed in variable


TSubclassOf<class UInventoryCell> InventoryCellClass

a link to the “Widget Bluprint”, when creating a widget InventoryWidget

And when calling


AddInventoryCell

the widget UInventoryCell class was created and placed in UUnoformGridPanel in “InventoryWidget”

InventoryWidget.h



#include "Blueprint/UserWidget.h"
#include "Components/UniformGridPanel.h"
#include "../../../InventoryCell.h"

public:

        UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
        class UUniformGridPanel* InventoryCells;

protected:

         virtual void NativeConstruct() override;

private:

        UPROPERTY(EditDefaultsOnly)
        TSubclassOf<class UInventoryCell> InventoryCellClass;

        UFUNCTION(BlueprintCallable)
         void AddInventoryCell(float SizeY, float SizeX, int32 PossitionY, int32 PossitionX);


InventoryWidget.cpp



void UInventoryWidget::NativeConstruct()
{
        Super::NativeConstruct();

        FStringClassReference InventoryCellClassRef(TEXT("/Game/TestInventoryCell"));
        InventoryCellClass = InventoryCellClassRef.TryLoadClass<UInventoryCell>();
}

void UInventoryWidget::AddInventoryCell(float SizeY, float SizeX, int32 PossitionY, int32 PossitionX)
{
      UInventoryCell* NewInventoryCell = CreateWidget<UInventoryCell>(this, InventoryCellClass);

      InventoryCells->AddChild(NewInventoryCell);
      NewInventoryCell->CreateInventoryCell(SizeY, SizeX, PossitionY, PossitionX);
}


InventoryCell.h



UFUNCTION(BlueprintCallable)
void CreateInventoryCell(float SizeY, float SizeX, int32 StartPossitionY, int32 StartPossitionX);


Server logged in
CreateWidget called with a null class.
Play in editor total start time 0,133 seconds.

Error at startup:

Why not just set the InventoryCellClass by hand in the editor? Hard coding assets into a system is never a good idea in my book. Also, I’m not sure if you are trying to use NativeOnConstruct as if it was the “constructor” of your class. Because as far as I know, NativeOnConstruct is called when you are creating the widget at runtime, after the widget has been constructed.



// InventoryWidget.h

public:

     /** Widget constructor. */
     UInventoryWidget(const FObjectInitializer& ObjectInitializer);

protected:

     /** Class to use for inventory cells. */
     UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Parameters")
     TSubclassOf<UInventoryCell> InventoryCellClass;




// InventoryWidget.cpp

UInventoryWidget::UInventoryWidget(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
     // You can give the TSubclassOf variable a default class to use like this.
     InventoryCellClass = UInventoryCell::StaticClass();
}


Thanks for the help. Everything works as it should.