locked
When using Attributes.Contains I get a NullReferenceException RRS feed

  • Question

  • I get the above error when the one field I am check for is null. Here is the code. Bold line is where I get my error.

     If context.InputParameters.Contains("Target") AndAlso TypeOf context.InputParameters("Target") Is Entity Then
                    Dim entity As Entity = DirectCast(context.InputParameters("Target"), Entity)
                    ' Verify that the entity represents a vehicle ownership.
                    If entity.LogicalName <> "esi_vehicleownership" Then
                        Throw New InvalidPluginExecutionException("The pre-image is not of esi_vehicleownership type.")
                    Else
                        newrefid = True
                        entityid = entity.Id 'status = entity.Attributes("esi_Status").value
                        status = entity.GetAttributeValue(Of Integer)("esi_Status")

                        If entity.Attributes.Contains("esi_vehicle") Then

                            vehicleid = entity.GetAttributeValue(Of EntityReference)("esi_vehicle").Id
                            vehicleref = New EntityReference("esi_car", vehicleid)
                        Else
                            vehicleid = Nothing
                            vehicleref = New EntityReference("esi_car", vehicleid)
                        End If

    Error:

    Unhandled Exception: Microsoft.Crm.CrmException: Unexpected exception from plug-in (Execute): UpdateVehicleOwnership.GetVehicleInfo: System.NullReferenceException: Object reference not set to an instance of an object.

    How do I get an null reference error when I'm trying to see if the field exists. I'm only referring to the Attributes object which is not null.

    Wednesday, February 4, 2015 10:03 PM

All replies

  • replace If entity.Attributes.Contains("esi_vehicle") Then

    on If entity.Contains("esi_vehicle") Then

    It should help.
    • Edited by xjomanx Thursday, February 5, 2015 7:24 AM
    Thursday, February 5, 2015 7:23 AM
  • Thanks for the reply unfortunately I get the same error.

    Funny thing is if I do entity.attributes.count I get a value so that object has been initialized.

    Thursday, February 5, 2015 2:13 PM
  • Work around that I implemented was I did a fetch on the entity instead of using the form entity. All the above code works in that scenario.

    Not efficient coding but its not an over used plugin so I'll live with it for now.


    • Edited by bbuchanan Thursday, February 5, 2015 3:40 PM
    Thursday, February 5, 2015 2:54 PM
  • Are you sure that line throws the error ? The line above (copied below) looks more likely to be the cause, as you're not testing for existence, and you've got an upper-case character in the name, while all logical names are lower case

    status = entity.GetAttributeValue(Of Integer)("esi_Status")
    


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Thursday, February 5, 2015 5:29 PM
    Moderator
  • Are you sure that line throws the error ? The line above (copied below) looks more likely to be the cause, as you're not testing for existence, and you've got an upper-case character in the name, while all logical names are lower case

    status = entity.GetAttributeValue(Of Integer)("esi_Status")


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk


    Any case, error should not raise. It should be true or false, but not error.
    Thursday, February 5, 2015 6:03 PM
  • Try to debug plugin. There will be clear, what is happening.

    How debug a plugin, you can see here:

    http://www.youtube.com/watch?v=Xr0Cb-GZssY

    at time: 14:20

    Thursday, February 5, 2015 6:11 PM