# UScrollBox Nullptr when AddChild c++

**URL:** <https://forums.unrealengine.com/t/uscrollbox-nullptr-when-addchild-c/1323691>\
**Category:** Programming & Scripting\
**Tags:** scrollbox, unreal-engine, Widget, CPP, question\
**Created:** [October 5, 2023, 9:12pm UTC](https://forums.unrealengine.com/t/uscrollbox-nullptr-when-addchild-c/1323691 "2023-10-05T21:12:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Itsnotme4](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Itsnotme4](https://forums.unrealengine.com/u/Itsnotme4)\
**Post date:** [October 5, 2023, 9:12pm UTC](https://forums.unrealengine.com/t/uscrollbox-nullptr-when-addchild-c/1323691/1 "2023-10-05T21:12:48Z")

</div>

I’m trying to populate a UScrollBox with Images using c++. The images are stored in a DataTable. I get a Nullptr when I try to add UColorButton to my scrollbox. Using Debug Tool Widget Reflector I can see that that children are added with Y size 0, but still null when I hit play.

![ButtonNullptr](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/4/c/84c956cbcb25e7fc47220bfd7f61e8b3b32a415e.png)

cpp for ColorButton, I get NullPtr in here

```auto
#include "UserInterface/Construction/ColorButton.h"
#include "Components/Image.h"

void UColorButton::NativeConstruct()
{
    Super::NativeConstruct();
    bIsFocusable = true;
}

UColorButton::UColorButton(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
}

void UColorButton::SetTexture(UTexture2D* NewTexture)
{
    if (IconImage)
    {
        UE_LOG(LogTemp, Warning, TEXT("IconImage is not a nullptr"));
        IconImage->SetBrushFromTexture(NewTexture);
    }
    else {
        UE_LOG(LogTemp, Warning, TEXT("IconImage is a nullptr")); //This happens
    }
}

```

header

```auto
#pragma once

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

class UImage;

UCLASS()
class MyGame_API UColorButton : public UUserWidget
{
	GENERATED_BODY()
public:
	UColorButton(const FObjectInitializer& ObjectInitializer);

	UFUNCTION(BlueprintCallable, Category = "Texture Widget")
	void SetTexture(UTexture2D* NewTexture);

	UPROPERTY(meta = (BindWidget))
	UImage* IconImage;
protected:
	virtual void NativeConstruct() override;
};

```

Then I create the widget inside my UConstructionWidget. NullPtr when I call “SetTexture”

```auto
void UConstructionUserWidget::NativeConstruct()
{
	Super::NativeConstruct();
	for (auto& it : ColorIconDataTable->GetRowMap()) {
		UColorButton* ColorImage = CreateWidget<UColorButton>(this, UColorButton::StaticClass());

		FWidgetDataStruct* Row = ColorIconDataTable->FindRow<FWidgetDataStruct>(it.Key, TEXT(""));

		if (Row){
			//ScrollBox
			UTexture2D* TextureFromDataTable = Row->Icon.IconImage;
			ColorImage->SetTexture(TextureFromDataTable);
			ColorWheelScroll->AddChild(ColorImage);
		}
	}

```

header of UConstructionWidget

```auto
	UPROPERTY(VisibleAnywhere, meta = (BindWidget), Category = "Construction")
	UScrollBox* ColorWheelScroll;

	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	UDataTable* ColorIconDataTable;

```

I have created Blueprints of both of them. And used the meta binding. What am I missing?

---

<div class="post-metadata">

**Author:** ![Itsnotme4](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Itsnotme4](https://forums.unrealengine.com/u/Itsnotme4)\
**Post date:** [October 6, 2023, 8:19am UTC](https://forums.unrealengine.com/t/uscrollbox-nullptr-when-addchild-c/1323691/2 "2023-10-06T08:19:23Z")

</div>

Found it! There was no binding to the WBP\_ColorButton. In my ConstructionWidget header I added

```auto
	UPROPERTY(EditDefaultsOnly, Category = "Construction Widget")
	TSubclassOf<UColorButton> ColorButtonClass;

```

in the cpp I updated with my subclass instead of staticclass

```auto
	ColorImage = CreateWidget<UColorButton>(this, ColorButtonClass);

```

Then I could bind my WBP\_ColorButton to my WBP\_ConstructionWidget inside of the Blueprint editor graph class defaults details.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [November 5, 2023, 8:20am UTC](https://forums.unrealengine.com/t/uscrollbox-nullptr-when-addchild-c/1323691/3 "2023-11-05T08:20:03Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
