Subobjects can't be accessed inside function

Subobjects created at constructor can’t be accessed inside a custom made function.
At Constructor:

 	// 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;
	UTextRenderComponent *CurrentChatRender = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CurrentChat"));
	CurrentChatRender->SetText(FText::FromString(CurrentChat));
	CurrentChatRender->SetHorizontalAlignment(EHTA_Left);
	CurrentChatRender->SetVerticalAlignment(EVRTA_TextTop);

At Constructor, subobjects were able to be modified, also in begin_play(), it didn’t seem to cause errors.
However inside a custom function UpdateChat(),

void AChatBotClass::UpdateChat(ChatType Chat ) {

	OldChatRender->SetText(FText::FromString(ChatLog));
}

Resulted in a error with the following message.

Exception thrown at 0x000007FED8C2B2A0 (UE4Editor-Core.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x0000000000000718.
Unhandled exception at 0x000007FED8C2B2A0 (UE4Editor-Core.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x0000000000000718.

Do I need to give a class pointer of CurrentChatRender to the function?

Hello! These exceptions are related to erroneous pointer logic/arithmetic. There is not enough contextual information for us to help you, so you might want to post more relevant code, or you could go ahead and protect all of your pointers. Eventually, one protection might fail and you can start from there.

By “protecting a pointer” I mean setting it as the null pointer where appropriate and checking for non-null correctness before dereferencing it, through a function call or using the built-in operator* using a method that allows you to see where/when/how it failed, such as UE4’s ensure(…) macro.

CurrentChatRender and OldChatRender are not the same variable. What’s going on there?

Also, does this crash immediately or does it work for a while and then crash?

is right: there’s not enough information for me to know what the answer is. You should also be building for DebugGame so the errors are clearer. You can also debug it in VS. If you’re using the editor then you want “DebugGame Editor”.

Hey! Sorry that I couldn’t answer sooner for the additional information.

In ChatBotClass.h

class CHATBOT_API AChatBotClass : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AChatBotClass();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	UPROPERTY()
		class UTextRenderComponent* CurrentChatRender;
	UPROPERTY()
		class UTextRenderComponent* OldChatRender;
public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	UFUNCTION()
		void UpdateChat(ChatType Chat);
	UPROPERTY()
		FString CurrentChat;
};

In ChatBotClass.cpp

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

#include "ChatBotClass.h"


// Sets default values
AChatBotClass::AChatBotClass()
{
 	// 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;
	UTextRenderComponent *CurrentChatRender = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CurrentChat"));
	CurrentChatRender->SetText(FText::FromString(CurrentChat));
	CurrentChatRender->SetHorizontalAlignment(EHTA_Left);
	CurrentChatRender->SetVerticalAlignment(EVRTA_TextTop);
	CurrentChatRender->SetRelativeLocation(FVector(0.0, 0.0, 0.0));
}

// Called when the game starts or when spawned
void AChatBotClass::BeginPlay()
{
	Super::BeginPlay();
	UpdateChat(Normal);
}
...
void AChatBotClass::UpdateChat(ChatType Chat) {

	switch (Chat) {
		case Normal:
			CurrentChat = ChatList[0];
		case Ban:
			CurrentChat = ChatList[0];
		case Help:
			CurrentChat = ChatList[0];
	}
	CurrentChatRender->SetText(FText::FromString(CurrentChat));

}

That’s all the code I’ve used. I’ve changed the place of UpdateChat from the constructor to begin_play to test if that was the problem, but apparently it didn’t do any good. I’m currently using microsoft visual studio 2017.

Hey, I figured it out. I only needed to put the declaration of class pointers right under GENERATED_BODY()

When you see this type of exception with a # very near to zero it is most likely a NULL class instance pointer, and you are indexing into that NULL space where you are not allowed.

Access violation reading location 0x0000000000000718