VB.Net Compiler Warning.......'used before assigned a value. null exception could result'.
-
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/
-
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/
-
daveauld wrote:
Dim dbReader As OleDb.OleDbDataReader
This should read:
Dim dbReader As OleDb.OleDbDataReader = New OleDb.OleDbDataReader
Regards: DidiDon'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/
-
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/
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
-
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
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/
-
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/
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.
-
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/
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