What might cause this NilObjectException when adding to a dictionary?

The method below is passed an XMLDocument (DefaultValues), and loops through it to extract values used to populate a variety of popup menus.

These key/value pairs are then stored in a set of dictionaries (which are Properties inside this module).

However, when it gets to the first case in the Select Case block, “Gauge” it’s crashing with a NilObjectException. I can’t figure out what’s causing this though - I don’t think it’s the values I’m passing to the dictionary, because those appear to be correct (see the two variables xmlID and xmlValue, which should be 0 and 28mm, respectively). The debugger is showing that those are correct. So, what am I missing here?

Var root As XmlNode
root = DefaultValues.DocumentElement
Var defaultElement As XmlNode

For c As Integer = 0 To root.ChildCount-1
  defaultElement = root.Child(c)
  //there may not be any data here, so check that first
  if defaultElement.FirstChild <> nil and defaultElement.FirstChild.Value <> "" then
    var xmlID as string = defaultElement.GetAttribute("id")
    var xmlValue as string = defaultElement.FirstChild.Value
    Select Case defaultElement.Name
    case "Gauge"
      DefaultFilmGauges.Value(xmlID) = xmlValue
    case "ColorMode"
      DefaultColorModes.Value(xmlID) = xmlValue
    case "ColorDepth"
      DefaultColorDepths.Value(xmlID) = xmlValue
    case "HDRMode"
      DefaultHDRMode.Value(xmlID) = xmlValue
    case "OutputFileFormat"
      DefaultOutputFileFormats.Value(xmlID) = xmlValue
    end select
    //if this is a comment, ignore it:
  elseif defaultElement.Name = "#comment" then 
  end if

This results in a NilObjectException on

DefaultFilmGauges.Value(xmlID) = xmlValue

But the exception is ErrorNumber 0, and has no message or reason. I can clearly see, though, that the data I’m looking for in xmlID and xmlValue is correct:

Could it be that DefaultFilmGauges is nil ?

If it is a property, you still need to initiate it like

DefaultFilmGauges = New Dictionary
1 Like

Doh! Yes. you’re correct. I completely forgot to do that.

Without changing anything, debug your project and click on NilObjectException. you may get a clue as to what is wrong.

Well like I said above, it doesn’t give me a reason or a message when I do that.