locked
Error with code - Unhandled exception nullreferenceexception RRS feed

  • Question

  • An unhandled exception of type 'System.NullReferenceException' occurred in program.exe
    Additional information: Object reference not set to an instance of an object.

    My.Settings.Drives.Add("500GB 6GB/s SATA III 32MB 7200RPM HDD")
    My.Settings.Drives.Add("24x 6GB/s SATA III DVD Writer")

    I can't figure out why it is doing this? Any help would be greatly appreciated. Thanks


    Wednesday, June 1, 2016 9:45 AM

Answers

  • Additional information: Object reference not set to an instance of an object.

    My.Settings.Drives appears to be a collection of some sort.  Has it been initialized to an empty collection?  Note that only some settings variables are automatically initialized when they are created - with others you need to provide an initialization value.

    If you don't initialize the settings variable when you create it, then you can test it for Nothing immediately prior to the above code, and if necessary create it at that point.

    If My.Settings.Drives Is Nothing then My.Settings.Drives = New <whatever>
    Wednesday, June 1, 2016 9:52 AM
  • As Acamar pointed out, any collection will have to instantiated. 

    If My.Settings.Drives is System.Collections.Specialized.StringCollection I prefer to use List(Of String) in my program and convert Specialized.StringCollection to the list at program load time and back to Specialized.StringCollection at close.  This assumes that the number of items is small.  Lists are a lot more flexible.

    This illustrates my point

            'during load
            If My.Settings.Drives Is Nothing Then
                My.Settings.Drives = New System.Collections.Specialized.StringCollection
            End If
            Dim myDriveList As List(Of String) = My.Settings.Drives.Cast(Of String).ToList()
    
            'using the list
            myDriveList.Add("500GB 6GB/s SATA III 32MB 7200RPM HDD")
            myDriveList.Add("24x 6GB/s SATA III DVD Writer")
    
            'at close
            My.Settings.Drives.Clear()
            My.Settings.Drives.AddRange(myDriveList.ToArray)


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.



    Wednesday, June 1, 2016 11:14 AM

All replies

  • The drives setting is set to nothing before this, but for every other setting which is only a basic string setting, there's no problem. There is only an issue for string collection settings.
    Wednesday, June 1, 2016 9:51 AM
  • Additional information: Object reference not set to an instance of an object.

    My.Settings.Drives appears to be a collection of some sort.  Has it been initialized to an empty collection?  Note that only some settings variables are automatically initialized when they are created - with others you need to provide an initialization value.

    If you don't initialize the settings variable when you create it, then you can test it for Nothing immediately prior to the above code, and if necessary create it at that point.

    If My.Settings.Drives Is Nothing then My.Settings.Drives = New <whatever>
    Wednesday, June 1, 2016 9:52 AM
  • As Acamar pointed out, any collection will have to instantiated. 

    If My.Settings.Drives is System.Collections.Specialized.StringCollection I prefer to use List(Of String) in my program and convert Specialized.StringCollection to the list at program load time and back to Specialized.StringCollection at close.  This assumes that the number of items is small.  Lists are a lot more flexible.

    This illustrates my point

            'during load
            If My.Settings.Drives Is Nothing Then
                My.Settings.Drives = New System.Collections.Specialized.StringCollection
            End If
            Dim myDriveList As List(Of String) = My.Settings.Drives.Cast(Of String).ToList()
    
            'using the list
            myDriveList.Add("500GB 6GB/s SATA III 32MB 7200RPM HDD")
            myDriveList.Add("24x 6GB/s SATA III DVD Writer")
    
            'at close
            My.Settings.Drives.Clear()
            My.Settings.Drives.AddRange(myDriveList.ToArray)


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.



    Wednesday, June 1, 2016 11:14 AM