Reference surface read as null

Hello guys, my question is kinda basic here but it has been screwing my head up for hours and hours

I’m developping this flocking plugin based on Long Nguyen’s definition for flocking.
I’m converting a 2d flock into a flock to UV coordinates.

the surface is well referenced in rhino to grasshopper. but when I get into my class it gives me a null reference exception

any help would be much appreciate I’ve been trying for hours and no luck

Here is my difinition:

namespace SurfaceTrails2.FlockingOnSrf
{
public class GhcFlockingOnSrf : GH_Component
{
private FlockSystem flockSystem;

    public GhcFlockingOnSrf()
        : base(
              "Flocking on surface",
              "FlockOnSrf",
              "Creates a swarm on a surface",
              "YFAtools",
              "AgentBased")
    {
    }


    protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
    {
        pManager.AddBooleanParameter("Reset", "Reset", "Reset", GH_ParamAccess.item, false);
        pManager.AddBooleanParameter("Play", "Play", "Play", GH_ParamAccess.item, false);
        //pManager.AddBooleanParameter("3D", "3D", "3D", GH_ParamAccess.item, true);
        pManager.AddSurfaceParameter("srf", "srf", "srf", GH_ParamAccess.item);
        pManager.AddIntegerParameter("Count", "Count", "Number of Agents", GH_ParamAccess.item, 50);
        pManager.AddNumberParameter("Timestep", "Timestep", "Timestep", GH_ParamAccess.item, 0.02);
        pManager.AddNumberParameter("Neighbourhood Radius", "Neighbourhood Radius", "Neighbourhood Radius", GH_ParamAccess.item, 3.5);
        pManager.AddNumberParameter("Alignment", "Alignment", "Alignment", GH_ParamAccess.item, 0.5);
        pManager.AddNumberParameter("Cohesion", "Cohesion", "Cohesion", GH_ParamAccess.item, 0.5);
        pManager.AddNumberParameter("Separation", "Separation", "Separation", GH_ParamAccess.item, 0.5);
        pManager.AddNumberParameter("Separation Distance", "Separation Distance", "Separation Distance", GH_ParamAccess.item, 1.5);
        pManager.AddCircleParameter("Repellers", "Repellers", "Repellers", GH_ParamAccess.list);
        pManager[10].Optional = true;
        pManager.AddBooleanParameter("Use Parallel", "Use Parallel", "Use Parallel", GH_ParamAccess.item, false);
        pManager.AddBooleanParameter("Use R-Tree", "Use R-Tree", "Use R-Tree", GH_ParamAccess.item, false);
    }


    protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
    {
        pManager.AddTextParameter("Info", "Info", "Information", GH_ParamAccess.item);
        pManager.AddPointParameter("Positions", "Positions", "The agent positions", GH_ParamAccess.list);
        pManager.AddVectorParameter("Velocities", "Velocities", "The agent veloctiies", GH_ParamAccess.list);
    }


    protected override void SolveInstance(IGH_DataAccess DA)
    {
        // ===============================================================================================
        // Read input parameters
        // ===============================================================================================

         bool iReset = true;
        bool iPlay = false;
        //bool i3D = false;
        Surface surface = null;
        int iCount = 0;
        double iTimestep = 0.0;
        double iNeighbourhoodRadius = 0.0;
        double iAlignment = 0.0;
        double iCohesion = 0.0;
        double iSeparation = 0.0;
        double iSeparationDistance = 0.0;
        List<Circle> iRepellers = new List<Circle>();
        bool iUseParallel = false;
        bool iUseRTree = false;

        DA.GetData("Reset", ref iReset);
        DA.GetData("Play", ref iPlay);
        //DA.GetData("3D", ref i3D);
        DA.GetData("srf", ref surface);
        DA.GetData("Count", ref iCount);
        DA.GetData("Timestep", ref iTimestep);
        DA.GetData("Neighbourhood Radius", ref iNeighbourhoodRadius);
        DA.GetData("Alignment", ref iAlignment);
        DA.GetData("Cohesion", ref iCohesion);
        DA.GetData("Separation", ref iSeparation);
        DA.GetData("Separation Distance", ref iSeparationDistance);
        DA.GetDataList("Repellers", iRepellers);
        DA.GetData("Use Parallel", ref iUseParallel);
        DA.GetData("Use R-Tree", ref iUseRTree);


        // ===============================================================================================
        // Read input parameters
        // ===============================================================================================


        if (iReset || flockSystem == null)
        {
            flockSystem = new FlockSystem(iCount/*, i3D*/,surface);
        }
        else
        {
            // ===============================================================================================
            // Assign the input parameters to the corresponding variables in the  "flockSystem" object
            // ===============================================================================================

            flockSystem.Timestep = iTimestep;
            flockSystem.NeighbourhoodRadius = iNeighbourhoodRadius;
            flockSystem.AlignmentStrength = iAlignment;
            flockSystem.CohesionStrength = iCohesion;
            flockSystem.SeparationStrength = iSeparation;
            flockSystem.SeparationDistance = iSeparationDistance;
            flockSystem.Repellers = iRepellers;
            flockSystem.UseParallel = iUseParallel;


            // ===============================================================================
            // Update the flock
            // ===============================================================================

            if (iUseRTree)
                flockSystem.UpdateUsingRTree();
            else
                flockSystem.Update();

            if (iPlay) ExpireSolution(true);
        }

        // ===============================================================================
        // Output the agent positions and velocities so we can see them on display
        // ===============================================================================

        // Reparameterize Surface (You're not reparameterizing the surface here)
      

        List<GH_Point> positions = new List<GH_Point>();
        List<GH_Vector> velocities = new List<GH_Vector>();

        foreach (FlockAgent agent in flockSystem.Agents)
        {
            var pointToTranslate = agent.Position;
            positions.Add(new GH_Point(surface.PointAt(pointToTranslate.X, pointToTranslate.Y)));
            velocities.Add(new GH_Vector(agent.Velocity));
        }

        DA.SetDataList("Positions", positions);
        DA.SetDataList("Velocities", velocities);
    }

and here is the class that won’t reference correctly

Thanks.

Use NurbsSurface. Don’t think Surface can be public, it is protected.
Capture
https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Surface__ctor.htm

So don’t use it as public or go to NurbsSurface: NurbsSurface nurbsSurface = surface.ToNurbsSurface();

1 Like

Thank you Michael that worked !