Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Plugin exception: Target not null but Object reference cannot be found

(0) ShareShare
ReportReport
Posted on by 355

Hello,

I created a plugin which is triggered when a user tries to deactivate an entity Movie; it should block the process if the movie is associated to an active entity of type Collection.

When I tried to deactivate some Movies the operation was always successful, no matter if they were related to an active Collection.

I'm debugging it and noticed that the current target is recognized, the following variable is not null:

var target = context.GetTarget();

Nonetheless, the following instruction throws the System.NullReferenceException

Entity currentMovie = (Entity)context.InputParameters["Target"];


Can you please help me to understand why I get that exception and if the code is correct? I used if (collectionStateCode.Value == 0) to check if the state is active, is it correct?

Here is my code:

        public void Execute(IServiceProvider serviceProvider)
        {
            var context = new PluginContext(serviceProvider);
            var service = context.CrmServiceSystem;
            var trace = context.TracingService;
            var target = context.GetTarget();

            if (target == null)
                return;

            Entity currentMovie = (Entity)context.InputParameters["Target";

            // 2) Assuming that collection is a required field on the movie, get the collectionid
            Guid collectionid= currentMovie .GetAttributeValue<EntityReference>("collection").Id;

                                    // 3) Query the encyclopedia for the details I need:
            var collectionEntity = service.Retrieve("collection", collectionid, new ColumnSet("statecode"));

                                     // 4) get the values from the collection entity        
            OptionSetValue collectionStateCode = (OptionSetValue)collectionEntity .Attributes["statecode";
            if (collectionStateCode.Value == 0) 
            {
                throw new InvalidPluginExecutionException(OperationStatus.Failed,, "The movie cannot be disable because it is associated to an active collection.");
            }
        }

  • Joel D Profile Picture
    Joel D 355 on at
    RE: Plugin exception: Target not null but Object reference cannot be found

    Ok, it looks like the input parameters contain Target and target is an entity: I can go through the block of code inside the if statement, but the exception is thrown after the service declaration, it seems:

    error_5F00_context_5F00_2.png

  • RE: Plugin exception: Target not null but Object reference cannot be found

    ["Target"] is Entity.

    Modify the code like mentioned above.

  • Joel D Profile Picture
    Joel D 355 on at
    RE: Plugin exception: Target not null but Object reference cannot be found

    Thank you Prakash.

    I added the condition under the context declaration but I got the following errors:

    error_5F00_context_5F00_1.png

  • RE: Plugin exception: Target not null but Object reference cannot be found

    Hi Joel,

    Code looks fine to me.

    Can you check one condition, whether the target is entity or not and look for input parameters.

     if(context.InputParameters.Contains("Target")&&

                context.InputParameters["Target"]isEntity)

    Regards,

    Prakash

  • Joel D Profile Picture
    Joel D 355 on at
    RE: Plugin exception: Target not null but Object reference cannot be found

    Hi Prakash,

    thank you.

    I followed your tip,I got a System.NullReferenceException, but now it was triggered before the declaration of the service, i.e. next to the following line of code:

    .

                var crmService = factory.CreateOrganizationService(context.UserId);
    

    I also noticed that the context is not null but it shows the following errors:

    error_5F00_context.png

    This is the updated code:

     IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    
                var crmService = factory.CreateOrganizationService(context.UserId);
               
                Entity currentMovie = (Entity)context.InputParameters["Target"];
    
                Guid collectionId = currentMovie.GetAttributeValue("collectionId").Id;
    
                var collectionEntity = crmService.Retrieve("collectionId", collectionId, new ColumnSet("statecode"));
    
                OptionSetValue collectionStateCode = (OptionSetValue)collectionEntity.Attributes["statecode"];            
    
                if (collectionStateCode.Value == 0)  
                {
                    throw new InvalidPluginExecutionException(OperationStatus.Failed, "The movie cannot be disable because it is associated to an active collection.");
                }

  • RE: Plugin exception: Target not null but Object reference cannot be found

    Hi Joel,

    Can you please create context object using this way.

    IPluginExecutionContext context =(IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

    It looks like something wrong with context object.



    Regards,
    Prakash

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans