CreateDefaultSubobject returns nullptr

I am just trying a very simple code but getting nullptr (I guess).

This is a part of my header file

public:
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UPROPERTY(VisibleAnywhere)
	class UShapeComponent* CloseTrigger;

And this is the constructor of my cpp file

ACat::ACat()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	CloseTrigger = CreateDefaultSubobject<UShapeComponent>(TEXT("Close Trigger"));
	if (CloseTrigger != nullptr)
	{
		CloseTrigger->SetupAttachment(RootComponent);
		UE_LOG(LogTemp, Display, TEXT("closetrigger successful "));
	}
	else
	{
		UE_LOG(LogTemp, Error, TEXT("closetrigger nullptr "));
	}

}

And I am getting the error log every time I open my derived blueprint or start the game.

I looked at this forum, google, reddit but only thing I found is that there is a bug in the engine but there is not a real solution.

Can anyone help?

Are you sure you can use UShapeComponent in that way? it is abstract so it has no implementation of it’s own

UCLASS(abstract, hidecategories=(Object,LOD,Lighting,TextureStreaming,Activation,“Components|Activation”), editinlinenew, meta=(BlueprintSpawnableComponent), showcategories=(Mobility))
class ENGINE_API UShapeComponent : public UPrimitiveComponent
{
}

Maybe you are thinking of ATriggerBase or one of it’s decedents?

If you want overlap detection then try a direct shape like UBoxComponent or USphereComponent etc

1 Like

Thank you!

I was supposed to put USphereComponent in there.
Before doing this, I was trying to do something depending on inheritence and I never noticed that I was trying to create an object of an abstract class.

I find developing C++ on Unreal really stressfull. I am trying to learn C++ by developing on Unreal but the fact that Unreal has its own macros, garbage collector and stuff on top of C++ is a little bit confusing for a noob. In addition to that, if you do something wrong, the whole engine just crashes and wouldn’t open again until you fix the error in your code.

I don’t want to flood this forum with lot’s of questions like this but I guess since I don’t have a senior with me all the time, I have to ask time to time.

On the other hand you have to start somewhere :wink: Don’t get discouraged, it gets easier with time.

Just have to watch out for thise pesky null pointers. Accessing a null variable is usually the main culprit of crashes.

A good tip is to encompas casts in if statements to check if the block of code using it will be successfull.

At least Unreal saves us from memory managment and object life cycles.