locked
Null reference exception

    Question

  • After upgrading my VB6 project, I get a long list of the following warning:

    Variable 'xyz' is passed by reference before it has been assigned a value. A null reference exception could result at runtime.

    This is a common and valid situation. It seems tedious to add a statement initializing the variable to 0 or null just to do away with the warning message. There are so many of them that the maximum limit is exceeded, possibly obscuring other warnings that I would like to see. Is there any way to turn off a specific type of warning message? Or is there a way to see the rest of my warning messages and filter this one out?

    Dan
    Thursday, June 14, 2007 7:41 PM

Answers

  • Suppressing warnings is generally a bad thing.

     

    I would encourage you to take the time to initialise all these variables to some suitable value (even null if null is appropriate).  Yes it seems tedious but you'll be glad you did the first time the warning identifies a case where not initialising the variable was in fact a bug.  Especially if the warning saves you from shipping buggy code to your customer.

     

    That said, there are two ways to suppress a warning.  The vbc compiler supports a /nowarn switch which can be used to supply a comma separated list of warning numbers you want suppressed.  The Visual Studio IDE allows you to choose whether certain conditions should generate an error, warning or nothing.  One of those conditions is "Use of variable prior to assignment".

     

    How to: Enable or Disable Compiler Warnings 

    http://msdn2.microsoft.com/en-us/library/edzzzth4(VS.80).aspx

    Thursday, June 14, 2007 9:46 PM
    Moderator
  • To initialise a structure you can either explicitly assign values to the structure instance after declaring it (which isn't terribly elegant) or you can create a constructor for the structure and use that constructor in the declaraction.  For example something like this

     


    Public Structure systemInfo
       Public cPU As String
       Public memory As Long
       Public diskDrives() As String
       Public purchaseDate As Date
     
       Public Sub New(ByVal v1 As String, ByVal v2 As Long, _
                               ByVal v3() As String, ByVal v4 As Date)
          cPU = v1
          memory = v2
          ReDim diskDrives(v3.Length - 1)
          Array.Copy(v3, diskDrives, v3.Length)
          purchaseDate = v4
       End Sub

    End Structure
     

    Dim s As systemInfo = New systemInfo(String.Empty, 0, _
       New String() {String.Empty}, Date.MinValue)

     

     

    It sounds like you could either adapt your initialisation subroutine to become the constructor or you could call the initislisation subroutine from the constructor.  One thing to watch out for, you can't create a parameterless constructor for a structure so your constructor has to take at least one parameter.

    Friday, June 15, 2007 3:38 AM
    Moderator

All replies

  • Suppressing warnings is generally a bad thing.

     

    I would encourage you to take the time to initialise all these variables to some suitable value (even null if null is appropriate).  Yes it seems tedious but you'll be glad you did the first time the warning identifies a case where not initialising the variable was in fact a bug.  Especially if the warning saves you from shipping buggy code to your customer.

     

    That said, there are two ways to suppress a warning.  The vbc compiler supports a /nowarn switch which can be used to supply a comma separated list of warning numbers you want suppressed.  The Visual Studio IDE allows you to choose whether certain conditions should generate an error, warning or nothing.  One of those conditions is "Use of variable prior to assignment".

     

    How to: Enable or Disable Compiler Warnings 

    http://msdn2.microsoft.com/en-us/library/edzzzth4(VS.80).aspx

    Thursday, June 14, 2007 9:46 PM
    Moderator
  • Frank,

    Thank you for your reply. I understand what you're saying and will try to initialize them all before using them in a sub or function call.

    How do I initialize a variable that is a structure type? I have a subroutine to initialize its members, but I can't call the subroutine to initialize it without generating the same warning.

    Dan
    Thursday, June 14, 2007 10:44 PM
  • To initialise a structure you can either explicitly assign values to the structure instance after declaring it (which isn't terribly elegant) or you can create a constructor for the structure and use that constructor in the declaraction.  For example something like this

     


    Public Structure systemInfo
       Public cPU As String
       Public memory As Long
       Public diskDrives() As String
       Public purchaseDate As Date
     
       Public Sub New(ByVal v1 As String, ByVal v2 As Long, _
                               ByVal v3() As String, ByVal v4 As Date)
          cPU = v1
          memory = v2
          ReDim diskDrives(v3.Length - 1)
          Array.Copy(v3, diskDrives, v3.Length)
          purchaseDate = v4
       End Sub

    End Structure
     

    Dim s As systemInfo = New systemInfo(String.Empty, 0, _
       New String() {String.Empty}, Date.MinValue)

     

     

    It sounds like you could either adapt your initialisation subroutine to become the constructor or you could call the initislisation subroutine from the constructor.  One thing to watch out for, you can't create a parameterless constructor for a structure so your constructor has to take at least one parameter.

    Friday, June 15, 2007 3:38 AM
    Moderator