Adding new entity generates OpenAccessException: "Object reference not set to an instance of an object."

3 posts, 1 answers
  1. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 26 Feb 2013 Link to this post

    Hello,

    Today I found a pretty weird problem. It started with an error from OpenAccess:
    OpenAccessException: "Object reference not set to an instance of an object."

    The problem was on a fairly simple entity add action:

    Dim obj_Item As Entities.App.Com.ItemGroup
     
    Using DbC As New ProoPortal.Connection("ProoPortalConnection01", True, True)
        obj_Item = New Entities.App.Com.ItemGroup
     
        With obj_Item
            .Number = rtb_Number.Text
            .Name = rtb_Name.Text
    ....
            .ItemGroupDefinitionId = If((rcb_ItemGroupDef.SelectedValue & "") = "", Nothing, rcb_ItemGroupDef.SelectedValue)
        End With
        Try
    >>>>>>> DbC.Add(obj_Item)
            DbC.SaveChanges(Telerik.OpenAccess.ConcurrencyConflictsProcessingMode.StopOnFirst)
        Catch ex As Exception
            If Diagnostics.Debugger.IsAttached Then Diagnostics.Debugger.Break()
        End Try
    End Using

    The error occurred on the adding of the object (DbC.Add(obj_Item))
    And not a selfexplaining error also...

    The problem was that the property ItemGroupDefinitionId was 0 instead of the Nothing    that was given back by the If(
    So, when entering the property Set:
    Private l_ItemGroupDefinitionId As Long?
    Public Property [ItemGroupDefinitionId]() As Long?
        Get
            Return l_ItemGroupDefinitionId
        End Get
        Set(ByVal aobj_Value As Long?)
            l_ItemGroupDefinitionId = aobj_Value
        End Set
    End Property ' ItemGroupDefinitionId
    The value was 0, while debugging clearly stated that the If( was returning Nothing!

    I changed the If(  toIifand the problem was solved...

    So, for others that may come across this... This was my afternoon... :-)


    Erik
  2. Answer
    Dimitar Tachev
    Admin
    Dimitar Tachev avatar
    67 posts

    Posted 04 Mar 2013 Link to this post

    Hello Erik,

     
    Getting 0 when using the If operator and Nothing with the IIf function is expected because in VB.NET Nothing can be applied both to value and to reference types and setting it is the same as setting the Default one. For example:

    ' integerNumber will be initialized with 0 (the default one for Long)
    Dim integerNumber As Long = Nothing

    The arguments of the ternary conditional operator (the If one) in VB are of type Object and in order to find the type of the result, the operator is considering its True and False parts.

    In your case the True part is Nothing (typeless), the False part is Long and as a result the return value is set to be Long (it is compatible with Nothing). Then when you get the True condition you are getting 0 because the result (of type Long) is returned with value of Nothing (which is converted to its default value (0) as in the code-snipped above).

    IIf is a function just like any other function (not an operator) which is created when the ternary conditional operator was still not implemented in VB (as its alternative) and it isn't more recommended.

    In order to get the Nothing value and use the If operator I suggest you to use one of the following approaches:

     1) Manually cast the Nothing value to the type you need.
    If((rcb_ItemGroupDef.SelectedValue & "") = "",
        CType(Nothing, Long?), rcb_ItemGroupDef.SelectedValue)

     2) Return a new instance of Nullable Long (with its default value)
    If((rcb_ItemGroupDef.SelectedValue & "") = "",
        New Long?(), rcb_ItemGroupDef.SelectedValue)

    I hope this helps. Do not hesitate to contact us again if you experience any further issues.


    All the best,
    Dimitar Tachev
    the Telerik team
    OpenAccess ORM Q1 2013 is out featuring Multi-Diagrams, Persistent Data Stream Support and much more. Sign up for a free webinar to see all the new stuff in action.
  3. DevCraft banner
  4. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 04 Mar 2013 Link to this post

    Hello Dimitar ,

    Thanks for your reply. And yes, you are right of course regarding the VB.NET If operator vs IIf function.

    But mostly I posted it because the OA error is far from self explaining... I now had an entity with 10 props, but when I had one with 50 it would take me a long time to find what's going on... So, I thought, let's post it so others can possibly benefit.

    Thanks again Dimitar.
Back to Top