Question Null reference exception??

bifteki

New member
Joined
Jul 24, 2009
Messages
4
Programming Experience
Beginner
I have written a CLR stored procedure which I call through code in MS Access.
It throws a Null Reference Exception. I guess it must be due to one of two variables I use inside the CLR procedure, for both of which the Refactor gives me a message 'Variable is used before it has been assigned a value. A null reference exception could result at runtime'.
I have assigned values to these variables and it worked fine for other parameters I executed the procedure with. But I changed the parameters and now I get the error.

Does anyone know why this may be happening?

Below I give you just the lines of the code where these variables are mentioned:

Dim emailBody As String

SQLcmd.CommandText = "SELECT fld_content FROM dbo.tbl_email_content WHERE fld_content_id = " & CStr(contID)
SQLcmd.CommandType = CommandType.Text
SQLcmd.Connection = SQLConn

SQLConn.Open()

RD = SQLcmd.ExecuteReader()

If RD.Read() Then
emailBody = RD(0).ToString
End If

SQLConn.Close()

SQLcmd.CommandText = "SELECT dbo.tbl_Persons.fld_prosfonisi, dbo.tbl_Persons.fld_pros, dbo.tbl_Companies.fld_company_formal_name " & _
"FROM dbo.itbl_company_person INNER JOIN " & _
"dbo.tbl_Persons ON dbo.itbl_company_person.fld_person_id = dbo.tbl_Persons.fld_person_id INNER JOIN " & _
"dbo.tbl_sent_history ON dbo.tbl_Persons.fld_person_id = dbo.tbl_sent_history.fld_person_id INNER JOIN " & _
"dbo.tbl_Companies ON dbo.itbl_company_person.fld_company_id = dbo.tbl_Companies.fld_company_id " & _
"WHERE (dbo.tbl_sent_history.fld_mass_sendID = '" & sentID & "')"

SQLcmd.CommandType = CommandType.Text
SQLcmd.Connection = SQLConn

SQLConn.Open()

RD = SQLcmd.ExecuteReader()

RD.Read()
tmpEmailBody = emailBody 'This line is where I get the message
emailBody = Replace(tmpEmailBody, "!@#$$#@!", RD(0).ToString)

SQLConn.Close()



And this one's for the second variable (an alternate view for an e-mail to be sent):

Dim htmlView As AlternateView

htmlView = AlternateView.CreateAlternateViewFromString(emailBody, System.Text.Encoding.UTF8, "text/html")
Mail.AlternateViews.Add(htmlView)
htmlView.Dispose() 'This is where I get the error
 
If you place your code in code tags it makes it much easier to read.


Couple of things... I don't see where you are declaring tmpEmailBody. But for emailBody you are decaring it but only assigning a value if 'RD' is readable.

VB.NET:
If RD.Read() Then
emailBody = RD(0).ToString
End If

Thats why you are getting the warning. If RD.Read fails then you never assign a value to emailBody.
 
I have declared tmpEmailBody, I just didn't paste the declaration line, I didn't want to paste a lot of reduntant code.

Actually I figured out why I got the refactor message: It's because the code that assigns a value to these two variables is within a loop (this isn't obvious in the code I pasted for the second variable but the same goes for it too). So I get a warning because the code might not enter the loop and the variable might not get a value.

The 'null reference exception' was a problem in some other line as it turned out. :p
 
Back
Top