c#_objRef to TextEntity()

Hi All,
I am trying to convert objRef to TextEnity(). I want to get the text written in TextEntity(). objRef is basically an annotation. Please let me know what I am doing wrong. It throws an error saying “Object reference not set to an instance of an object” .

Here is the code.

I have more than hundred files I want to read specific annotation text from those. Name of the annotation in all the file is same, only the annotation text is different. PLease let me know how to proceed further

        File3dm thisFile = Rhino.FileIO.File3dm.Read(filePath);  // read file with given path
     
        Rhino.FileIO.File3dmObjectTable oT = thisFile.Objects;

        foreach(Rhino.FileIO.File3dmObject o in oT)
        {
            string name = o.Name;
            if (name == "EBENER_PLANKOPF_BBOX")
            {
                Guid id = o.Id;
                Rhino.DocObjects.ObjRef objref = new ObjRef(id);
                TextEntity textentity = objref.TextEntity();
                string text = textentity.PlainText;
                RhinoApp.WriteLine(text);
            }
        }

Ok I Solved it!

            if (name == "EBENER_PLANKOPF_BBOX")
            {
                Guid id = o.Id;

                //RhinoDoc doc; // available from e.g. a command or RhinoDoc.ActiveDoc
                TextEntity entity = o.Geometry as Rhino.Geometry.TextEntity;
                string text = entity.PlainText;
               // doc.Objects.Add(o.Geometry, o.Attributes);
                RhinoApp.WriteLine(text);
            }