-
Aug 7th, 2013, 08:44 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] NullReferenceException and I don't know why.
I am getting a nullreferenceexception when I try running this code below.
vbNET Code:
Private _listOfItems As List(Of Item)
''' <summary>
''' This will check for ItemAttachments and then add the item to a list.
''' </summary>
''' <param name="item">Item to check for attachments</param>
''' <remarks></remarks>
Private Sub CheckForItemAttachments(ByVal item As Item)
If item.HasAttachments Then
_listOfItems.Add(item)' Errors right here with A first chance exception of type 'System.NullReferenceException' occurred in ISSvc2.exe
End If
End Sub
RATE THOSE WHO ARE HELPFUL PLEASE!!
-
Aug 7th, 2013, 08:52 AM
#2
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.
-
Aug 7th, 2013, 08:59 AM
#3
Thread Starter
Fanatic Member
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:
Private _listOfItems As New List(Of Item)
RATE THOSE WHO ARE HELPFUL PLEASE!!
-
Aug 7th, 2013, 09:00 AM
#4
Thread Starter
Fanatic Member
Re: NullReferenceException and I don't know why.
I'm confused, because this is what worked
vbNET Code:
Private _listOfItems As List(Of Item) ''' <summary> ''' This will check for ItemAttachments and then add them to _listOfItems(Of Item). ''' </summary> ''' <param name="item">Item to check for attachments</param> ''' <remarks></remarks> Private Sub CheckForItemAttachments(ByVal item As Item) If item.HasAttachments Then Try _listOfItems = New List(Of Item) _listOfItems.Add(item) Catch ex As NullReferenceException Debug.Print(ex.InnerException.ToString) End Try End If End Sub
I don't want to create a new list each time.
RATE THOSE WHO ARE HELPFUL PLEASE!!
-
Aug 7th, 2013, 09:03 AM
#5
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)
-
Aug 7th, 2013, 09:05 AM
#6
Thread Starter
Fanatic Member
Re: NullReferenceException and I don't know why.
Originally Posted by dday9
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!!
-
Aug 7th, 2013, 09:06 AM
#7
Re: NullReferenceException and I don't know why.
Does the exception get thrown in your try/catch or is the NullReferenceException?
-
Aug 7th, 2013, 09:07 AM
#8
Thread Starter
Fanatic Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|