Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. VB.Net Compiler Warning.......'used before assigned a value. null exception could result'.

VB.Net Compiler Warning.......'used before assigned a value. null exception could result'.

Scheduled Pinned Locked Moved Visual Basic
csharpdatabasecomhelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DaveAuld
    wrote on last edited by
    #1

    Hi folks, I am looking for a bit of guidance on the best way to structure the following code to prevent the compiler warning. The code works and does what it should, i just don't want to have the warning message sitting there all the time. Bascially, the code polls a database for a list of values and returns them to the caller as a list(of String).

    Public Function GetPlatformList() As List(Of String)
    
        Dim result As New List(Of String)
        Dim dbCon As New OleDb.OleDbConnection(dbConnectionString)
    
        Dim dbCMD As OleDb.OleDbCommand
        Dim dbReader As OleDb.OleDbDataReader
    
        dbCMD = New OleDb.OleDbCommand("SELECT PlatformName FROM Platforms", dbCon)
    
        Try
            dbCon.Open()
    
            dbReader = dbCMD.ExecuteReader()
            Dim HasResult As Boolean = True
            Do Until Not HasResult
                Do While dbReader.Read()
                    result.Add(dbReader.GetString(0))
                Loop
                HasResult = dbReader.NextResult()
            Loop
    
        Catch ex As Exception
            'Error Trying
            MsgBox("Error Accessing Database: " & ex.Message, MsgBoxStyle.Critical, "Database Error")
        End Try
    
        dbReader.Close()
        dbCon.Close()
    
        'Return the list of platforms
        Return result
    End Function
    

    I have tried coding this different ways, and this is following the examples in MSDN etc, but i always end up with the compiler waring;

    Variable 'dbReader' is used before it has been assigned a value. A null reference exception could result at runtime.

    The guidance I am looking for is how to structure it in such a way as to prevent the warning. Thanks, dave

    Dave Who am I?: http://www.bebo.com/daveauld/ or http://www.dave-auld.net/

    D L C 3 Replies Last reply
    0
    • D DaveAuld

      Hi folks, I am looking for a bit of guidance on the best way to structure the following code to prevent the compiler warning. The code works and does what it should, i just don't want to have the warning message sitting there all the time. Bascially, the code polls a database for a list of values and returns them to the caller as a list(of String).

      Public Function GetPlatformList() As List(Of String)
      
          Dim result As New List(Of String)
          Dim dbCon As New OleDb.OleDbConnection(dbConnectionString)
      
          Dim dbCMD As OleDb.OleDbCommand
          Dim dbReader As OleDb.OleDbDataReader
      
          dbCMD = New OleDb.OleDbCommand("SELECT PlatformName FROM Platforms", dbCon)
      
          Try
              dbCon.Open()
      
              dbReader = dbCMD.ExecuteReader()
              Dim HasResult As Boolean = True
              Do Until Not HasResult
                  Do While dbReader.Read()
                      result.Add(dbReader.GetString(0))
                  Loop
                  HasResult = dbReader.NextResult()
              Loop
      
          Catch ex As Exception
              'Error Trying
              MsgBox("Error Accessing Database: " & ex.Message, MsgBoxStyle.Critical, "Database Error")
          End Try
      
          dbReader.Close()
          dbCon.Close()
      
          'Return the list of platforms
          Return result
      End Function
      

      I have tried coding this different ways, and this is following the examples in MSDN etc, but i always end up with the compiler waring;

      Variable 'dbReader' is used before it has been assigned a value. A null reference exception could result at runtime.

      The guidance I am looking for is how to structure it in such a way as to prevent the warning. Thanks, dave

      Dave Who am I?: http://www.bebo.com/daveauld/ or http://www.dave-auld.net/

      D Offline
      D Offline
      DidiKunz
      wrote on last edited by
      #2

      daveauld wrote:

      Dim dbReader As OleDb.OleDbDataReader

      This should read: Dim dbReader As OleDb.OleDbDataReader = New OleDb.OleDbDataReader Regards: Didi

      D 1 Reply Last reply
      0
      • D DidiKunz

        daveauld wrote:

        Dim dbReader As OleDb.OleDbDataReader

        This should read: Dim dbReader As OleDb.OleDbDataReader = New OleDb.OleDbDataReader Regards: Didi

        D Offline
        D Offline
        DaveAuld
        wrote on last edited by
        #3

        Don't think so, as that cause a 'No Constructors error' and also if you read the spec for OleDbCommand.ExecuteReader, it returns a new reader with the result set.

        Dave Who am I?: http://www.bebo.com/daveauld/ or http://www.dave-auld.net/

        1 Reply Last reply
        0
        • D DaveAuld

          Hi folks, I am looking for a bit of guidance on the best way to structure the following code to prevent the compiler warning. The code works and does what it should, i just don't want to have the warning message sitting there all the time. Bascially, the code polls a database for a list of values and returns them to the caller as a list(of String).

          Public Function GetPlatformList() As List(Of String)
          
              Dim result As New List(Of String)
              Dim dbCon As New OleDb.OleDbConnection(dbConnectionString)
          
              Dim dbCMD As OleDb.OleDbCommand
              Dim dbReader As OleDb.OleDbDataReader
          
              dbCMD = New OleDb.OleDbCommand("SELECT PlatformName FROM Platforms", dbCon)
          
              Try
                  dbCon.Open()
          
                  dbReader = dbCMD.ExecuteReader()
                  Dim HasResult As Boolean = True
                  Do Until Not HasResult
                      Do While dbReader.Read()
                          result.Add(dbReader.GetString(0))
                      Loop
                      HasResult = dbReader.NextResult()
                  Loop
          
              Catch ex As Exception
                  'Error Trying
                  MsgBox("Error Accessing Database: " & ex.Message, MsgBoxStyle.Critical, "Database Error")
              End Try
          
              dbReader.Close()
              dbCon.Close()
          
              'Return the list of platforms
              Return result
          End Function
          

          I have tried coding this different ways, and this is following the examples in MSDN etc, but i always end up with the compiler waring;

          Variable 'dbReader' is used before it has been assigned a value. A null reference exception could result at runtime.

          The guidance I am looking for is how to structure it in such a way as to prevent the warning. Thanks, dave

          Dave Who am I?: http://www.bebo.com/daveauld/ or http://www.dave-auld.net/

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi Dave, the problem is the try block may be interrupted by an exception, so the compiler isn't sure the <code>dbReader = dbCMD.ExecuteReader()</code> will be executed, in which case <code>dbReader.Close()</code> uses dbReader which never obtained a value. The solution to this is: 1. declare dbReader outside of the try block (you have that) 2. AND set it to Nothing 3. apply "test-before-use" outside the try block: If dbReader <> Nothing Then dbReader.Close() :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          modified on Friday, May 29, 2009 7:53 AM

          D 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi Dave, the problem is the try block may be interrupted by an exception, so the compiler isn't sure the <code>dbReader = dbCMD.ExecuteReader()</code> will be executed, in which case <code>dbReader.Close()</code> uses dbReader which never obtained a value. The solution to this is: 1. declare dbReader outside of the try block (you have that) 2. AND set it to Nothing 3. apply "test-before-use" outside the try block: If dbReader <> Nothing Then dbReader.Close() :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            modified on Friday, May 29, 2009 7:53 AM

            D Offline
            D Offline
            DaveAuld
            wrote on last edited by
            #5

            Thanks Luc. problem solved. I had completley forgot how the Nothing works, and for some reason had it in my mind that when setting something to nothing it could no longer be used. I won't forget again! (hopefully!). Just goes to show how if you are not a regular programmer (which i am definitely not) you forget the basics! Cheers Again, dave

            Dave Who am I?: http://www.bebo.com/daveauld/ or http://www.dave-auld.net/

            L 1 Reply Last reply
            0
            • D DaveAuld

              Thanks Luc. problem solved. I had completley forgot how the Nothing works, and for some reason had it in my mind that when setting something to nothing it could no longer be used. I won't forget again! (hopefully!). Just goes to show how if you are not a regular programmer (which i am definitely not) you forget the basics! Cheers Again, dave

              Dave Who am I?: http://www.bebo.com/daveauld/ or http://www.dave-auld.net/

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              You're welcome. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              1 Reply Last reply
              0
              • D DaveAuld

                Hi folks, I am looking for a bit of guidance on the best way to structure the following code to prevent the compiler warning. The code works and does what it should, i just don't want to have the warning message sitting there all the time. Bascially, the code polls a database for a list of values and returns them to the caller as a list(of String).

                Public Function GetPlatformList() As List(Of String)
                
                    Dim result As New List(Of String)
                    Dim dbCon As New OleDb.OleDbConnection(dbConnectionString)
                
                    Dim dbCMD As OleDb.OleDbCommand
                    Dim dbReader As OleDb.OleDbDataReader
                
                    dbCMD = New OleDb.OleDbCommand("SELECT PlatformName FROM Platforms", dbCon)
                
                    Try
                        dbCon.Open()
                
                        dbReader = dbCMD.ExecuteReader()
                        Dim HasResult As Boolean = True
                        Do Until Not HasResult
                            Do While dbReader.Read()
                                result.Add(dbReader.GetString(0))
                            Loop
                            HasResult = dbReader.NextResult()
                        Loop
                
                    Catch ex As Exception
                        'Error Trying
                        MsgBox("Error Accessing Database: " & ex.Message, MsgBoxStyle.Critical, "Database Error")
                    End Try
                
                    dbReader.Close()
                    dbCon.Close()
                
                    'Return the list of platforms
                    Return result
                End Function
                

                I have tried coding this different ways, and this is following the examples in MSDN etc, but i always end up with the compiler waring;

                Variable 'dbReader' is used before it has been assigned a value. A null reference exception could result at runtime.

                The guidance I am looking for is how to structure it in such a way as to prevent the warning. Thanks, dave

                Dave Who am I?: http://www.bebo.com/daveauld/ or http://www.dave-auld.net/

                C Offline
                C Offline
                chilinhhacker
                wrote on last edited by
                #7

                I'm not good at English Just ignore this warning, because you assigned a value to dbReader in Try Statement Think about Try: It means code in Try-Catch can be exited at any time.

                chilinhhacker

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups