472,327 Members | 1,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,327 software developers and data experts.

NullReferenceException when trying to fill an array

Hi,

I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :)

I am writing a windows service wich sends emails to customers at a specific time. My app retrieves cities and the city's associated time from a database, waits until the specified time for each city and sends out the appropriate emails. Below is some code that is generating a NullReferenceException which I haven't been able to debug. Please help!

Friend Class RgsDbConnection

Private rgsConn As SqlConnection
Private goTimes() As Date
Private locales() As String
Private _numLocales As Integer

....

Private Sub GetLocalesAndTimes()

Dim cmd As SqlDataAdapter
Dim rsData As New DataSet
Dim i As Integer

'Get a list of all locales and their email/fax sending time
cmd = New SqlDataAdapter("SELECT zonename, EmailFaxTime " _
& "FROM dbo.tblzone", me.rgsConn)
cmd.Fill(rsData, "LocalesAndTimes")

'Get number of locales
me._numLocales = rsData.Tables("LocalesAndTimes").Rows.Count

For i = 0 To me._numLocales - 1
'fill array with locales
Dim log as EventLog = New EventLog()
Try
'This next line is the one that causes the error
Me.locales(i) = _
rsData.Tables("LocalesAndTimes").Rows(i).Item("zon ename")
Catch e As Exception
log.WriteEntry("RGS Email & Fax Sender", _
rsData.Tables("LocalesAndTimes").Rows(i).Item("zon ename").ToString())
log.WriteEntry("RGS Email & Fax Sender", e.ToString)
End Try
'fill array with sending times
' me.goTimes(i) = _
' rsData.Tables("LocalesAndTimes").Rows(i).Item("Ema ilFaxTime")
Next i
End Sub
....

End Class

In the "For...Next" block you will notice a "Try" block. Inside the try block
is the single line of code that is causing the problem.

You'll notice in the catch block I write to the application event log twice (for each iteration of the for...next block). When I look at the event log, there are two entries for every record in my db, one stating the zonename and one with the error, which reads:
"System.NullReferenceException: Object reference not set to an instance of an object.
at RgsEmailSenderSpace.RgsDbConnection.GetLocalesAndT imes()"

Thanks in advance for your help.
fabio
Jul 21 '05 #1
5 2535
Try this:

If Not ISDBNULL(rsData.Tables("LocalesAndTimes").Rows(i). Item("zonename")
) Then
me.locales(1) = rsData.Tables("LocalesAndTimes").Rows(i).Item("zon ename")

Or this:

Debug.Assert(Not IsDBNull(rsData.Tables("LocalesAndTimes").Rows(i). Item("zonename")
))

If the assertion fails, you have a null value. This link will also show you a possible method to deal with it http://www.knowdotnet.com/testsite/nullvalues.html

You can also IIF that statement with the same logic and give it a different value.
"Fabio Papa" <fa****@hotmail.com> wrote in message news:bv*******************@news1.telusplanet.net.. .
Hi,

I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :)

I am writing a windows service wich sends emails to customers at a specific time. My app retrieves cities and the city's associated time from a database, waits until the specified time for each city and sends out the appropriate emails. Below is some code that is generating a NullReferenceException which I haven't been able to debug. Please help!

Friend Class RgsDbConnection

Private rgsConn As SqlConnection
Private goTimes() As Date
Private locales() As String
Private _numLocales As Integer

...

Private Sub GetLocalesAndTimes()

Dim cmd As SqlDataAdapter
Dim rsData As New DataSet
Dim i As Integer

'Get a list of all locales and their email/fax sending time
cmd = New SqlDataAdapter("SELECT zonename, EmailFaxTime " _
& "FROM dbo.tblzone", me.rgsConn)
cmd.Fill(rsData, "LocalesAndTimes")

'Get number of locales
me._numLocales = rsData.Tables("LocalesAndTimes").Rows.Count

For i = 0 To me._numLocales - 1
'fill array with locales
Dim log as EventLog = New EventLog()
Try
'This next line is the one that causes the error
Me.locales(i) = _
rsData.Tables("LocalesAndTimes").Rows(i).Item("zon ename")
Catch e As Exception
log.WriteEntry("RGS Email & Fax Sender", _
rsData.Tables("LocalesAndTimes").Rows(i).Item("zon ename").ToString())
log.WriteEntry("RGS Email & Fax Sender", e.ToString)
End Try
'fill array with sending times
' me.goTimes(i) = _
' rsData.Tables("LocalesAndTimes").Rows(i).Item("Ema ilFaxTime")
Next i
End Sub
...

End Class

In the "For...Next" block you will notice a "Try" block. Inside the try block
is the single line of code that is causing the problem.

You'll notice in the catch block I write to the application event log twice (for each iteration of the for...next block). When I look at the event log, there are two entries for every record in my db, one stating the zonename and one with the error, which reads:
"System.NullReferenceException: Object reference not set to an instance of an object.
at RgsEmailSenderSpace.RgsDbConnection.GetLocalesAndT imes()"

Thanks in advance for your help.
fabio
Jul 21 '05 #2
"Fabio Papa" <fa****@hotmail.com> schrieb
Private locales() As String
[...]
'This next line is the one that causes the error
Me.locales(i) = _
rsData.Tables("LocalesAndTimes").Rows(i).Item("zon ename")

You did not create an array of strings. Variable locales contains Nothing.
Use Redim to create an array.
--
Armin

Jul 21 '05 #3
Hi William, thanks for your help. However there are no nulls in the db (it's a test db with only five records that I have verified). Further, when i write the city to the event log (as in my catch block), or try to assign it to another variable (one that is not a member variable array), it works just fine (I can see the cities listed in the event log). Another piece to the puzzle is that the sdk documentation says this about the NullReferenceException exception:

"Note that applications throw the ArgumentNullException exception rather than the NullReferenceException exception discussed here. The following Microsoft intermediate language (MSIL) instructions throw NullReferenceException: ........"

From the little that I know about IL, it looks like the exception is being thrown by the stelem.ref instruction in my compiled code.
fabio
"William Ryan" <do********@comcast.nospam.net> wrote in message news:Oe*************@TK2MSFTNGP09.phx.gbl...
Try this:

If Not ISDBNULL(rsData.Tables("LocalesAndTimes").Rows(i). Item("zonename")
) Then
me.locales(1) = rsData.Tables("LocalesAndTimes").Rows(i).Item("zon ename")

Or this:

Debug.Assert(Not IsDBNull(rsData.Tables("LocalesAndTimes").Rows(i). Item("zonename")
))

If the assertion fails, you have a null value. This link will also show you a possible method to deal with it http://www.knowdotnet.com/testsite/nullvalues.html

You can also IIF that statement with the same logic and give it a different value.
"Fabio Papa" <fa****@hotmail.com> wrote in message news:bv*******************@news1.telusplanet.net.. .
Hi,

I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :)

I am writing a windows service wich sends emails to customers at a specific time. My app retrieves cities and the city's associated time from a database, waits until the specified time for each city and sends out the appropriate emails. Below is some code that is generating a NullReferenceException which I haven't been able to debug. Please help!

Friend Class RgsDbConnection

Private rgsConn As SqlConnection
Private goTimes() As Date
Private locales() As String
Private _numLocales As Integer

...

Private Sub GetLocalesAndTimes()

Dim cmd As SqlDataAdapter
Dim rsData As New DataSet
Dim i As Integer

'Get a list of all locales and their email/fax sending time
cmd = New SqlDataAdapter("SELECT zonename, EmailFaxTime " _
& "FROM dbo.tblzone", me.rgsConn)
cmd.Fill(rsData, "LocalesAndTimes")

'Get number of locales
me._numLocales = rsData.Tables("LocalesAndTimes").Rows.Count

For i = 0 To me._numLocales - 1
'fill array with locales
Dim log as EventLog = New EventLog()
Try
'This next line is the one that causes the error
Me.locales(i) = _
rsData.Tables("LocalesAndTimes").Rows(i).Item("zon ename")
Catch e As Exception
log.WriteEntry("RGS Email & Fax Sender", _
rsData.Tables("LocalesAndTimes").Rows(i).Item("zon ename").ToString())
log.WriteEntry("RGS Email & Fax Sender", e.ToString)
End Try
'fill array with sending times
' me.goTimes(i) = _
' rsData.Tables("LocalesAndTimes").Rows(i).Item("Ema ilFaxTime")
Next i
End Sub
...

End Class

In the "For...Next" block you will notice a "Try" block. Inside the try block
is the single line of code that is causing the problem.

You'll notice in the catch block I write to the application event log twice (for each iteration of the for...next block). When I look at the event log, there are two entries for every record in my db, one stating the zonename and one with the error, which reads:
"System.NullReferenceException: Object reference not set to an instance of an object.
at RgsEmailSenderSpace.RgsDbConnection.GetLocalesAndT imes()"

Thanks in advance for your help.
fabio
Jul 21 '05 #4
That worked! Thank you Armin.
"Armin Zingler" <az*******@freenet.de> wrote in message
news:ew**************@TK2MSFTNGP10.phx.gbl...
"Fabio Papa" <fa****@hotmail.com> schrieb
Private locales() As String
[...]
'This next line is the one that causes the error
Me.locales(i) = _
rsData.Tables("LocalesAndTimes").Rows(i).Item("zon ename")

You did not create an array of strings. Variable locales contains Nothing.
Use Redim to create an array.
--
Armin

Jul 21 '05 #5
redim your locals and goTimes arrays. Unless I am mistaken, they do
not automaticly do this

'Get number of locales
me._numLocales = rsData.Tables("LocalesAndTimes").Rows.Count
redim goTimes(_numlocals-1)
redim locales(_numlocals-1)

On Tue, 23 Sep 2003 15:18:31 GMT, "Fabio Papa" <fa****@hotmail.com>
wrote:
Hi,

I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :)

I am writing a windows service wich sends emails to customers at a specific time. My app retrieves cities and the city's associated time from a database, waits until the specified time for each city and sends out the appropriate emails. Below is some code that is generating a NullReferenceException which I haven't been able to debug. Please help!

Friend Class RgsDbConnection

Private rgsConn As SqlConnection
Private goTimes() As Date
Private locales() As String
Private _numLocales As Integer

...

Private Sub GetLocalesAndTimes()

Dim cmd As SqlDataAdapter
Dim rsData As New DataSet
Dim i As Integer

'Get a list of all locales and their email/fax sending time
cmd = New SqlDataAdapter("SELECT zonename, EmailFaxTime " _
& "FROM dbo.tblzone", me.rgsConn)
cmd.Fill(rsData, "LocalesAndTimes")

'Get number of locales
me._numLocales = rsData.Tables("LocalesAndTimes").Rows.Count

For i = 0 To me._numLocales - 1
'fill array with locales
Dim log as EventLog = New EventLog()
Try
'This next line is the one that causes the error
Me.locales(i) = _
rsData.Tables("LocalesAndTimes").Rows(i).Item("zon ename")
Catch e As Exception
log.WriteEntry("RGS Email & Fax Sender", _
rsData.Tables("LocalesAndTimes").Rows(i).Item("zon ename").ToString())
log.WriteEntry("RGS Email & Fax Sender", e.ToString)
End Try
'fill array with sending times
' me.goTimes(i) = _
' rsData.Tables("LocalesAndTimes").Rows(i).Item("Ema ilFaxTime")
Next i
End Sub
...

End Class

In the "For...Next" block you will notice a "Try" block. Inside the try block
is the single line of code that is causing the problem.

You'll notice in the catch block I write to the application event log twice (for each iteration of the for...next block). When I look at the event log, there are two entries for every record in my db, one stating the zonename and one with the error, which reads:
"System.NullReferenceException: Object reference not set to an instance of an object.
at RgsEmailSenderSpace.RgsDbConnection.GetLocalesAndT imes()"

Thanks in advance for your help.
fabio


Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: msnews.microsoft.com | last post by:
actaully the code i have written below is pretty simple and short. no idea why it terminate at run-time with "An unhandled exception of type...
3
by: Ryan Liu | last post by:
Hi there, I got a NullReferenceException when delete last row in a datagrid. I had hard time to solve since it does not occur in my own code. ...
1
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException....
2
by: sxiao | last post by:
Hi, there I got a NullReferenceException when there are more than one users trying to open the same page at the same time. The senerio is: Two...
6
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits...
5
by: Fabio Papa | last post by:
Hi, I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if...
5
by: ducky801 | last post by:
Hi all. When i run the code below i get a 'NullReferenceException was unhandled' error. I am using VB 2005 Express. I'm confused about this...
0
by: schoultzy | last post by:
Hello Everyone, I have been trying to figure this one out for two days now. I have created a DataGridView which is populated by an...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.