473,662 Members | 2,596 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NullReferenceEx ception 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 NullReferenceEx ception 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 GetLocalesAndTi mes()

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 , "LocalesAndTime s")

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

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 ("zonename")
Catch e As Exception
log.WriteEntry( "RGS Email & Fax Sender", _
rsData.Tables(" LocalesAndTimes ").Rows(i).Item ("zonename").To String())
log.WriteEntry( "RGS Email & Fax Sender", e.ToString)
End Try
'fill array with sending times
' me.goTimes(i) = _
' rsData.Tables(" LocalesAndTimes ").Rows(i).Item ("EmailFaxTime" )
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.NullRef erenceException : Object reference not set to an instance of an object.
at RgsEmailSenderS pace.RgsDbConne ction.GetLocale sAndTimes()"

Thanks in advance for your help.
fabio
Nov 20 '05 #1
5 3786
Try this:

If Not ISDBNULL(rsData .Tables("Locale sAndTimes").Row s(i).Item("zone name")
) Then
me.locales(1) = rsData.Tables(" LocalesAndTimes ").Rows(i).Item ("zonename")

Or this:

Debug.Assert(No t IsDBNull(rsData .Tables("Locale sAndTimes").Row s(i).Item("zone name")
))

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******** ***********@new s1.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 NullReferenceEx ception 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 GetLocalesAndTi mes()

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 , "LocalesAndTime s")

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

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 ("zonename")
Catch e As Exception
log.WriteEntry( "RGS Email & Fax Sender", _
rsData.Tables(" LocalesAndTimes ").Rows(i).Item ("zonename").To String())
log.WriteEntry( "RGS Email & Fax Sender", e.ToString)
End Try
'fill array with sending times
' me.goTimes(i) = _
' rsData.Tables(" LocalesAndTimes ").Rows(i).Item ("EmailFaxTime" )
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.NullRef erenceException : Object reference not set to an instance of an object.
at RgsEmailSenderS pace.RgsDbConne ction.GetLocale sAndTimes()"

Thanks in advance for your help.
fabio
Nov 20 '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 ("zonename")

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

Nov 20 '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 NullReferenceEx ception exception:

"Note that applications throw the ArgumentNullExc eption exception rather than the NullReferenceEx ception exception discussed here. The following Microsoft intermediate language (MSIL) instructions throw NullReferenceEx ception: ........"

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********@com cast.nospam.net > wrote in message news:Oe******** *****@TK2MSFTNG P09.phx.gbl...
Try this:

If Not ISDBNULL(rsData .Tables("Locale sAndTimes").Row s(i).Item("zone name")
) Then
me.locales(1) = rsData.Tables(" LocalesAndTimes ").Rows(i).Item ("zonename")

Or this:

Debug.Assert(No t IsDBNull(rsData .Tables("Locale sAndTimes").Row s(i).Item("zone name")
))

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******** ***********@new s1.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 NullReferenceEx ception 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 GetLocalesAndTi mes()

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 , "LocalesAndTime s")

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

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 ("zonename")
Catch e As Exception
log.WriteEntry( "RGS Email & Fax Sender", _
rsData.Tables(" LocalesAndTimes ").Rows(i).Item ("zonename").To String())
log.WriteEntry( "RGS Email & Fax Sender", e.ToString)
End Try
'fill array with sending times
' me.goTimes(i) = _
' rsData.Tables(" LocalesAndTimes ").Rows(i).Item ("EmailFaxTime" )
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.NullRef erenceException : Object reference not set to an instance of an object.
at RgsEmailSenderS pace.RgsDbConne ction.GetLocale sAndTimes()"

Thanks in advance for your help.
fabio
Nov 20 '05 #4
That worked! Thank you Armin.
"Armin Zingler" <az*******@free net.de> wrote in message
news:ew******** ******@TK2MSFTN GP10.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 ("zonename")

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

Nov 20 '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.Cou nt
redim goTimes(_numloc als-1)
redim locales(_numloc als-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 NullReferenceEx ception 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 GetLocalesAndTi mes()

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 , "LocalesAndTime s")

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

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 ("zonename")
Catch e As Exception
log.WriteEntry( "RGS Email & Fax Sender", _
rsData.Tables(" LocalesAndTimes ").Rows(i).Item ("zonename").To String())
log.WriteEntry( "RGS Email & Fax Sender", e.ToString)
End Try
'fill array with sending times
' me.goTimes(i) = _
' rsData.Tables(" LocalesAndTimes ").Rows(i).Item ("EmailFaxTime" )
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.NullRe ferenceExceptio n: Object reference not set to an instance of an object.
at RgsEmailSenderS pace.RgsDbConne ction.GetLocale sAndTimes()"

Thanks in advance for your help.
fabio


Nov 20 '05 #6

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

Similar topics

5
2632
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 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...
3
1727
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 'System.NullReferenceException' occurred" Thank you ---code below--- using System;
0
1975
by: franz | last post by:
Hello I have tabbed UI - each tab is based on UserControl. On userControl I have datagrid connected to dataset - filled from odbcDataAdapter. The OnLoad Event is: odbcDataAdapter1.Fill(dataSetCustomers1); This is how I add new tab in MainWindow (when click on button):
3
5032
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. I put a datagrid in my inherited user control, then put this control on a form. I use DataAdaptor to fill the data table and update database.
1
4085
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. ---------------------------------------------------------------------------- ------------------------------------------- Public Class TestClass Public NextSubIndex As Integer = 1 Public arrTestSubClass() As TestSubClass
2
2762
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 users logged into the web application using the Windows domain user account and tried to open the same page which has the databasde query code. When the two user tried to open the same page at the same time, there is a runtime error messsage showing as follows:
6
22208
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 System.Web.UI.Page Public Class ReportCardData Public Structure Attend Dim DaysTardy As Double
5
6592
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 because, I have declared my array and i'm trying to put things in it, so shouldn't it BE NULL until i start to put things in it? I'm kind of new to this so hoepfully i'm just overlooking something easy. I know my code is actually talking to the DB (or dataset rather) because the messageboxes work....
0
2304
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 ObjectDataSource. My problem occurs when I attempt to use the Edit feature of the DataGridView. I have set several of the BoundField elements of the DataGridView to ReadOnly="True". When I attempt to use the Edit feature to UPDATE the rows in the DataGridView I get a System.NullReferenceException for...
0
8435
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8547
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.