NullReferenceException when trying to fill an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fabio Papa

    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 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


  • William Ryan

    #2
    Re: NullReferenceEx ception when trying to fill an array

    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" <fapapa@hotmail .com> wrote in message news:bvZbb.4459 $H86.116878@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


    Comment

    • Armin Zingler

      #3
      Re: NullReferenceEx ception when trying to fill an array

      "Fabio Papa" <fapapa@hotmail .com> schrieb[color=blue]
      > Private locales() As String
      > [...]
      > 'This next line is the one that causes the error
      > Me.locales(i) = _
      > rsData.Tables(" LocalesAndTimes ").Rows(i).Item ("zonename")[/color]


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


      --
      Armin

      Comment

      • Fabio Papa

        #4
        Re: NullReferenceEx ception when trying to fill an array

        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" <dotnetguru@com cast.nospam.net > wrote in message news:OejHTfegDH A.696@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" <fapapa@hotmail .com> wrote in message news:bvZbb.4459 $H86.116878@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


        Comment

        • Fabio Papa

          #5
          Re: NullReferenceEx ception when trying to fill an array

          That worked! Thank you Armin.
          "Armin Zingler" <az.nospam@free net.de> wrote in message
          news:ewY7aregDH A.2072@TK2MSFTN GP10.phx.gbl...[color=blue]
          > "Fabio Papa" <fapapa@hotmail .com> schrieb[color=green]
          > > Private locales() As String
          > > [...]
          > > 'This next line is the one that causes the error
          > > Me.locales(i) = _
          > > rsData.Tables(" LocalesAndTimes ").Rows(i).Item ("zonename")[/color]
          >
          >
          > You did not create an array of strings. Variable locales contains Nothing.
          > Use Redim to create an array.
          >
          >
          > --
          > Armin
          >[/color]


          Comment

          • Charles

            #6
            Re: NullReferenceEx ception when trying to fill an array

            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" <fapapa@hotmail .com>
            wrote:
            [color=blue]
            >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[/color]

            Comment

            Working...