Cannot figure out what is wrong with this code to change the material of a basic mesh in C++

Thanks Guys for the help. I have tried all the suggestions, of which moving the code was one I was going to give it a go, but within the constructor itself. It compiles and runs without crashing in most of my gos, but I haven’t got the material to change. And it seems UE4.12 handles the project crashing well. I have not had to recover the project from backup. And since No one has indicated that the code I supplied is wrong. I’ll have to assume it is correct, and I just have to find out what goes where to get it to work. Sorry about the formatting, the editor messed up the tabs in the copy and paste and changed things as I was creating the post.

My understanding and experience of UE is just at the very start and so I am trying simple tasks which is all I need to do anyway right now. My line of thinking of how things work will have to change it seems as well. When starting out I thought loading a material or texture in c++ would simply be.

1 : load material/texture file structure into memory and have a pointer point to it.
2 : run a function to overwrite whatever material/texture currently exists that the mesh has allocated to it.

or

run a function for a particular object that one is changing the material/texture on that has the path name to the material or texture file as its parameter.

Alas there seems to be a much more complicated method of design philosophy and execution than I anticipated. And then there is the possibility of legacy issues that each iteration having to deal with old code that can cause problems in simplifying or adding functionality when writing code.

If this is a sign of things to come I may have dived into a pool that is too deep for me to swim in.
Oh well. Back to trying to find out how to get this to work.

Regards

DominioN