I know that a null reference exception generally occurs when accessing an item in a collection that doesn't exist. However, in this case, one is being thrown despite me explicitly creating this item mere lines beforehand. I have scoured my code and cannot find the source of this error (the code is very basic ATM as I have just begun the process of restructuring a solution made for a client.
Background info:
PhotoJobs is a custom class that holds all of the properties of a specific manufacturing Job
These are all held in a
Public Dictionary(of string, PhotoJob)
which is held in the MainForm class.- A ("temp") photojob is created within this dictionary to handle the addition of new jobs as data is added (this appears to be the source of the error.
Code:
Private Sub AddJob_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MainForm.photoJobs.Add("temp", New PhotoJob())
pctBox.AllowDrop = True
End Sub
Public Sub pctbox_drop(sender As Object, e As DragEventArgs) Handles pctBox.DragDrop
Dim pics As String() = CType(e.Data.GetData(DataFormats.FileDrop), String()) 'Gets the data from the file drop
If MainForm.photoJobs("temp").imageList.Count = 0 Then
MainForm.photoJobs("temp").imageList = pics.ToList 'Gets the data from the file drop
Else
For i = 0 To MainForm.photoJobs("temp").imageList.Count - 1
If Not MainForm.photoJobs("temp").imageList.Contains(pics(i)) Then
MainForm.photoJobs("temp").imageList.Add(pics(i))
End If
Next
End If
MainForm.photoJobs("temp").photoID = CType(formatID(MainForm.photoJobs("temp").imageList(0)), String)
txtPhotoID.Text = MainForm.photoJobs("temp").photoID
Select Case MainForm.photoJobs("temp").imageList.Count
Case 0
MsgBox("Please ensure that you are dropping image files")
Case 1
lblImageNumber.Text = txtPhotoID.Text
checkBoxes(0)
Case 2
txtPhotoID.Text = txtPhotoID.Text & "(2)"
lblImageNumber.Text = txtPhotoID.Text
checkBoxes(1)
Case 3
txtPhotoID.Text = txtPhotoID.Text & "(3)"
lblImageNumber.Text = txtPhotoID.Text
checkBoxes(2)
Case 4
txtPhotoID.Text = txtPhotoID.Text & "(4)"
lblImageNumber.Text = txtPhotoID.Text
checkBoxes(3)
End Select
spinCounter.Value = 1
spinCounter.Minimum = 1
spinCounter.Maximum = MainForm.photoJobs("temp").imageList.Count
pctBox.ImageLocation = MainForm.photoJobs("temp").imageList(0)
End Sub
The error gets called on the If MainForm.photoJobs("temp").imageList.Count = 0 Then
line.
On a second, minor note, is it fairly typical for clients to ask for "just one more little thing" that results in you having to make a major overhaul to an application, or have I just got unlucky? (slightly rhetorical)