Results 1 to 8 of 8

Thread: [RESOLVED] NullReferenceException and I don't know why.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Resolved [RESOLVED] NullReferenceException and I don't know why.

    I am getting a nullreferenceexception when I try running this code below.

    vbNET Code:
    1. Private _listOfItems As List(Of Item)
    2.  
    3.     ''' <summary>
    4.     ''' This will check for ItemAttachments and then add the item to a list.
    5.     ''' </summary>
    6.     ''' <param name="item">Item to check for attachments</param>
    7.     ''' <remarks></remarks>
    8.     Private Sub CheckForItemAttachments(ByVal item As Item)
    9.         If item.HasAttachments Then
    10.             _listOfItems.Add(item)' Errors right here with A first chance exception of type 'System.NullReferenceException' occurred in ISSvc2.exe
    11.  
    12.         End If
    13.     End Sub
    RATE THOSE WHO ARE HELPFUL PLEASE!!

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: NullReferenceException and I don't know why.

    You've declared your List(Of Item) but you haven't instantiated it with the New Operator.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: NullReferenceException and I don't know why.

    I haven't shown the code where I did instantiate it, but when I did the same issue occurred.

    Here is that code:

    vbNET Code:
    1. Private _listOfItems As New List(Of Item)
    RATE THOSE WHO ARE HELPFUL PLEASE!!

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: NullReferenceException and I don't know why.

    I'm confused, because this is what worked


    vbNET Code:
    1. Private _listOfItems As List(Of Item)
    2.  
    3.     ''' <summary>
    4.     ''' This will check for ItemAttachments and then add them to _listOfItems(Of Item).
    5.     ''' </summary>
    6.     ''' <param name="item">Item to check for attachments</param>
    7.     ''' <remarks></remarks>
    8.     Private Sub CheckForItemAttachments(ByVal item As Item)
    9.         If item.HasAttachments Then
    10.             Try
    11.                 _listOfItems = New List(Of Item)
    12.                 _listOfItems.Add(item)
    13.             Catch ex As NullReferenceException
    14.                 Debug.Print(ex.InnerException.ToString)
    15.             End Try
    16.  
    17.         End If
    18.     End Sub

    I don't want to create a new list each time.
    RATE THOSE WHO ARE HELPFUL PLEASE!!

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,714

    Re: NullReferenceException and I don't know why.

    You don't need to create a new list every time. Simply declare the list as a new list in line 1:
    Code:
    Private _listOfItems As New List(Of Item)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: NullReferenceException and I don't know why.

    Quote Originally Posted by dday9 View Post
    You don't need to create a new list every time. Simply declare the list as a new list in line 1:
    Code:
    Private _listOfItems As New List(Of Item)
    I noted above, that when I did do this, it fails with the same error.

    By the way, I like the little bug in your signature... it caught my attention.
    RATE THOSE WHO ARE HELPFUL PLEASE!!

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,714

    Re: NullReferenceException and I don't know why.

    Does the exception get thrown in your try/catch or is the NullReferenceException?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: NullReferenceException and I don't know why.

    Ok, so I swear it failed when I instantiated it like the above example... now it's not... interesting. Otherwise I wouldn't have made a post for this lol. I may have goofed, needless to say it is working.
    RATE THOSE WHO ARE HELPFUL PLEASE!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width