How to instantiate text on mouse click

Im trying to have a prefab of a text and then when i click on a box instantiate it, it works fine but the problem is i can’t get it to be a child of the canvas when instantiated, i tried using transform.setParent but it gives me object reference not set to an instance of an object.

How would i instantiate an instance of a text prefab with it being a child of the canvas?
Please help!

I figured it out i used an empty gameobject as a prefab and gave it a text component later in script…