Results 1 to 2 of 2

Thread: Error msg

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Error msg

    Hi All

    I've upgraded my program to .net and I'm getting this error.My program works fine but I'd like to understand If I'm doing something wrong.

    Reston



    Warning 1 Variable 'DDD' is used before it has been assigned a value. A null reference exception could result at runtime. C:\Documents and Settings\CJ\Final1\frm2nd.vb 315 111 Project1

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Error msg

    That's not an error message, it's a warning message, which is why the underline is green instead of blue and the icon in the Errors window is yellow instead of red. It means that you've declared a variable and then used it before it can be guaranteed to have been assigned a value, e.g.
    VB Code:
    1. Dim str As String
    2.  
    3. MessageBox.Show(str)
    In many cases this won't matter and the above is one of them. That's why it's a warning and not an error. The reason you are being warned is that it could result in a NullReferenceException in some circumstances, e.g.
    VB Code:
    1. Dim con As OleDb.OleDbConnection
    2.  
    3. con.Open()
    You should evaluate the possibilities and if a NullReferenceException could be thrown then thank the IDE for warning you and fix your code. If you know for sure that there will be no issue then to clear the warning message you simply initialise the variable, e.g.
    VB Code:
    1. Dim str As String = Nothing
    2.  
    3. MessageBox.Show(str)
    The variable would have been Nothing anyway but by explicitly setting it to Nothing you are telling the IDE that you want it to be so and you will take responsibility for ensuring that no NullReferenceExceptions result.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width