1

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)

ScottishTapWater
  • 3,656
  • 4
  • 38
  • 81
  • Maybe `imageList` is null, use the debugger, it tells you all you need to know. – Tim Schmelter May 01 '15 at 22:15
  • `imagelist` sounds like it is not an ImageList form component but a qualified collection. In that case, about the only cause there can be is that `photoJobs("temp")` is Nothing. The debugger will easily help you find it. SideNote: `MainForm` looks like a default form reference will will bite you sooner or later with many apps. – Ňɏssa Pøngjǣrdenlarp May 01 '15 at 23:14
  • Nearly all NullReference Exceptions have the same set of causes. See [NullReference Exception in Visual Basic](http://stackoverflow.com/a/26761773/1070452) for help on this. – Ňɏssa Pøngjǣrdenlarp May 01 '15 at 23:15

3 Answers3

1

Ensure that imageList is not null, as you declare a new PhotoJob but don't set any values in it.

When dealing with a NullReferenceException, you're not looking at something missing from a collection, it means you tried to access a member of a object which is null. This can sometimes be a side effect of an item not in a collection, if that collection returns null, but if a value does not exist in a Dictionary, you get a KeyNotFoundException

David
  • 10,458
  • 1
  • 28
  • 40
1

Please make sure that 'imageList' has 'count' property more than 0. Means just check that the list does have elements in it by executing theses lines before executing loop.

Dim pics As String() =CType(e.Data.GetData(DataFormats.FileDrop),String())

If MainForm.photoJobs("temp").imageList.Count > 0 Then

    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
    'Gets the data from the file drop
    Else
         MainForm.photoJobs("temp").imageList = pics.ToList                 
    End If
hdkhardik
  • 662
  • 3
  • 22
0

Well this is embarrassing...

The solution was simply that I was missing new when I declared the list it used to read Public Property imageList as list(of string) can't believe I did that... Hey ho, thanks for all of your help guys.

ScottishTapWater
  • 3,656
  • 4
  • 38
  • 81