Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object reference not set to an instance of an object Error

Status
Not open for further replies.

PsychoCoder

Programmer
May 31, 2006
140
US
OK,

I have created an error reporting class file for the application Im working on, in it I have the following procedure which emails both administrators of the error. This isnt called unless theres an error in a process on one of the pages. THe procedure goes as follows:

Code:
Public Shared Sub SendErrorNotification(ByVal user As Integer, ByVal page As String, ByVal process As String, ByVal err As Integer, ByVal message As String, ByVal type As String)
		With oMail
			.To.Add(New Net.Mail.MailAddress("[b]email_address_1[/b]", "[b]To Name 1[/b]"))
			.To.Add(New Net.Mail.MailAddress("[b]email_address_2[/b]", "[b]To Name 2[/b]"))
			.From = New Net.Mail.MailAddress("[b]from email[/b]", "[b]From name[/b]")
			.Subject = "Error in process: " & process & " on page: " & page
			.Body = [b]To Name 1[/b] & [b]To Name 2[/b],<br> There has been an error in the process " & process & " on page " & page & ".<BR>"
			.Body &= "The error information is as follows:<BR>-------------------------------------------------------------------------------<BR>"
			.Body &= "User: " & user & "<BR>"
			.Body &= "Error Code: " & err & "<BR>"
			.Body &= "Error Type: " & type & "<BR>"
			.Body &= "Error Message: " & message & "<BR>"
			.Body &= "Date: " & DateTime.Now.ToShortDateString & "<BR>"
			.Body &= " Time: " & DateTime.Now.ToShortTimeString & "<BR>"
			.Body &= "-------------------------------------------------------------------------------<BR>"
			.IsBodyHtml = True
		End With
		Dim Client As New Net.Mail.SmtpClient()
		Client.Host = "[b]server_name[/b]"
		Client.Send(oMail)
		oMail = Nothing
		Client = Nothing
	End Sub

When this is called on one of my pages I get the following error:

Code:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 72: 	Public Shared Sub SendErrorNotification(ByVal user As Integer, ByVal page As String, ByVal process As String, ByVal err As Integer, ByVal message As String, ByVal type As String)
Line 73: 		With oMail
Line 74: 			.To.Add(New Net.Mail.MailAddress("[b]email[/b]", "[b]name[/b]"))
Line 75: 			.To.Add(New Net.Mail.MailAddress("[b]email[/b]", "[b]name[/b]"))
Line 76: 			.From = New Net.Mail.MailAddress("[b]email[/b]", "[b]name[/b]")

This error doesnt occur on every page that the class is called, so far this is the only page this has happened on. any ideas anyone?

PS: The bold items are the real values, just removed for security
 
Sorry about that, at the top of the page I have:

Code:
Private Shared oMail As New Net.Mail.MailMessage

Sorry I forgot to include that.
 
Anyone have any ideas on how I can fix this problem? Like I said, it dont happen on all the pages.

Thank you for your help
 
hi,

which line is the .NET error page highlight?

the with oMail line???

Known is handfull, Unknown is worldfull
 
vbkris,

Yes its the With oMail line, what Ive done is change the declaration at the top to:
Code:
Private Shared oMail As Net.Mail.MailMessage

Then before the With oMail line I added:
Code:
oMail = New Net.Mail.MailMessage
to see if this solves anything. The weird part is it doesnt happen on ALL pages, just some.
 
vbkris,

The above fixed the problem, but it still dont explain why it only happened on certain pages.
 
the problem could be the because your code is not thread safe.

since all pages are trying to access the same shared variable at one shot this problem could be arising...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top