Problem with ReDim statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan Sokalski

    #1

    Problem with ReDim statement

    I have an Array that I will be resizing with the ReDim statement. For some
    reason that I cannot figure out, I am recieving the following error:

    [NullReferenceEx ception: Object reference not set to an instance of an
    object.]

    The code that this occurs in is as follows:

    Dim NavButton As NavButtonInfo

    Dim TempButtons() As NavButtonInfo

    For Each NavButton In ButtonList

    If NavButton.paren tname = tempparentname Then

    ReDim Preserve TempButtons(Tem pButtons.GetUpp erBound(0) + 1)

    TempButtons(Tem pButtons.GetUpp erBound(0)) = NavButton

    End If

    Next


    The error occurs on the ReDim line of this code. I am assuming that this has
    something to do with the fact that I initially declare TempButtons without
    giving it's size, but if I do not have the array the exact size (which is
    unknown until runtime) it will cause problems later in my code. For example,
    if I place constants like 10 and 5 in the Redim and following lines, it will
    get past this section of my code and then give me an error later in my code
    when it tries to read from indexes that have not been assigned values. Any
    help would be appreciated. Thanks.
    --
    Nathan Sokalski
    njsokalski@hotm ail.com
    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。



  • Cor Ligthert

    #2
    Re: Problem with ReDim statement

    Nathan,

    The problem with Redims problems is that there are not many people who use
    the Redim in dotNet.

    It real a method from VB6 and older. By instance the ArrayList or any other
    dynamic array give real much more performance, that it is worth to take that
    one of those and to recode a little bit.

    Here the arraylist, however there are much more collections/arrays/lists
    http://msdn.microsoft.com/library/de...classtopic.asp

    I hope this helps,

    Cor


    Comment

    • Charles Law

      #3
      Re: Problem with ReDim statement

      Hi Nathan

      Replace the line
      [color=blue]
      > Dim TempButtons() As NavButtonInfo[/color]

      with

      Dim TempButtons(-1) As NavButtonInfo

      This will initialise the array with no elements.

      HTH

      Charles


      "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
      news:eaQ9WCdWFH A.3840@tk2msftn gp13.phx.gbl...[color=blue]
      >I have an Array that I will be resizing with the ReDim statement. For some
      >reason that I cannot figure out, I am recieving the following error:
      >
      > [NullReferenceEx ception: Object reference not set to an instance of an
      > object.]
      >
      > The code that this occurs in is as follows:
      >
      > Dim NavButton As NavButtonInfo
      >
      > Dim TempButtons() As NavButtonInfo
      >
      > For Each NavButton In ButtonList
      >
      > If NavButton.paren tname = tempparentname Then
      >
      > ReDim Preserve TempButtons(Tem pButtons.GetUpp erBound(0) + 1)
      >
      > TempButtons(Tem pButtons.GetUpp erBound(0)) = NavButton
      >
      > End If
      >
      > Next
      >
      >
      > The error occurs on the ReDim line of this code. I am assuming that this
      > has something to do with the fact that I initially declare TempButtons
      > without giving it's size, but if I do not have the array the exact size
      > (which is unknown until runtime) it will cause problems later in my code.
      > For example, if I place constants like 10 and 5 in the Redim and following
      > lines, it will get past this section of my code and then give me an error
      > later in my code when it tries to read from indexes that have not been
      > assigned values. Any help would be appreciated. Thanks.
      > --
      > Nathan Sokalski
      > njsokalski@hotm ail.com
      > http://www.nathansokalski.com/
      >[/color]


      Comment

      • Jerry Spence1

        #4
        Re: Problem with ReDim statement

        You could also try

        Dim TempButtons() As New NavButtonInfo

        That seemed to work for me.

        -Jerry

        "Charles Law" <blank@nowhere. com> wrote in message
        news:%234xl1weW FHA.1796@TK2MSF TNGP15.phx.gbl. ..[color=blue]
        > Hi Nathan
        >
        > Replace the line
        >[color=green]
        >> Dim TempButtons() As NavButtonInfo[/color]
        >
        > with
        >
        > Dim TempButtons(-1) As NavButtonInfo
        >
        > This will initialise the array with no elements.
        >
        > HTH
        >
        > Charles
        >
        >
        > "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
        > news:eaQ9WCdWFH A.3840@tk2msftn gp13.phx.gbl...[color=green]
        >>I have an Array that I will be resizing with the ReDim statement. For some
        >>reason that I cannot figure out, I am recieving the following error:
        >>
        >> [NullReferenceEx ception: Object reference not set to an instance of an
        >> object.]
        >>
        >> The code that this occurs in is as follows:
        >>
        >> Dim NavButton As NavButtonInfo
        >>
        >> Dim TempButtons() As NavButtonInfo
        >>
        >> For Each NavButton In ButtonList
        >>
        >> If NavButton.paren tname = tempparentname Then
        >>
        >> ReDim Preserve TempButtons(Tem pButtons.GetUpp erBound(0) + 1)
        >>
        >> TempButtons(Tem pButtons.GetUpp erBound(0)) = NavButton
        >>
        >> End If
        >>
        >> Next
        >>
        >>
        >> The error occurs on the ReDim line of this code. I am assuming that this
        >> has something to do with the fact that I initially declare TempButtons
        >> without giving it's size, but if I do not have the array the exact size
        >> (which is unknown until runtime) it will cause problems later in my code.
        >> For example, if I place constants like 10 and 5 in the Redim and
        >> following lines, it will get past this section of my code and then give
        >> me an error later in my code when it tries to read from indexes that have
        >> not been assigned values. Any help would be appreciated. Thanks.
        >> --
        >> Nathan Sokalski
        >> njsokalski@hotm ail.com
        >> http://www.nathansokalski.com/
        >>[/color]
        >
        >[/color]


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Problem with ReDim statement

          "Charles Law" <blank@nowhere. com> schrieb:[color=blue]
          > Replace the line
          >[color=green]
          >> Dim TempButtons() As NavButtonInfo[/color]
          >
          > with
          >
          > Dim TempButtons(-1) As NavButtonInfo
          >
          > This will initialise the array with no elements.[/color]

          Both lines are semantically equal.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • Charles Law

            #6
            Re: Problem with ReDim statement

            Hi Herfried

            If they are semantically equal, why do they behave differently?

            In the first case, the NullReferenceEx ception exception is thrown, in the
            second case it is not.

            Charles


            "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
            news:%23JcFt8gW FHA.1040@TK2MSF TNGP10.phx.gbl. ..[color=blue]
            > "Charles Law" <blank@nowhere. com> schrieb:[color=green]
            >> Replace the line
            >>[color=darkred]
            >>> Dim TempButtons() As NavButtonInfo[/color]
            >>
            >> with
            >>
            >> Dim TempButtons(-1) As NavButtonInfo
            >>
            >> This will initialise the array with no elements.[/color]
            >
            > Both lines are semantically equal.
            >
            > --
            > M S Herfried K. Wagner
            > M V P <URL:http://dotnet.mvps.org/>
            > V B <URL:http://classicvb.org/petition/>[/color]


            Comment

            • Herfried K. Wagner [MVP]

              #7
              Re: Problem with ReDim statement

              "Charles Law" <blank@nowhere. com> schrieb:[color=blue]
              > If they are semantically equal, why do they behave differently?
              >
              > In the first case, the NullReferenceEx ception exception is thrown, in the
              > second case it is not.[/color]

              Sorry, my bad. I mixed the code up with these two lines which are
              semantically equivalent:

              \\\
              Dim b1() As Button = New Button() {}
              Dim b2() As Button = New Button(-1) {}
              ///

              --
              M S Herfried K. Wagner
              M V P <URL:http://dotnet.mvps.org/>
              V B <URL:http://classicvb.org/petition/>

              Comment

              • Armin Zingler

                #8
                Re: Problem with ReDim statement

                "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schrieb[color=blue]
                > "Charles Law" <blank@nowhere. com> schrieb:[color=green]
                >> Replace the line
                >>[color=darkred]
                >>> Dim TempButtons() As NavButtonInfo[/color]
                >>
                >> with
                >>
                >> Dim TempButtons(-1) As NavButtonInfo
                >>
                >> This will initialise the array with no elements.[/color]
                >
                > Both lines are semantically equal.[/color]

                1st doesn't create an array. 2nd creates an array with no items.

                Armin

                Comment

                • Nathan Sokalski

                  #9
                  Re: Problem with ReDim statement

                  When I use the line "Dim TempButtons() As New NavButtonInfo" I recieve the
                  following error when I build:

                  Arrays cannot be declared with 'New'.

                  --
                  Nathan Sokalski
                  njsokalski@hotm ail.com
                  有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


                  "Jerry Spence1" <jerry.spence@s omewhere.com> wrote in message
                  news:4288681d$0 $533$ed2619ec@p tn-nntp-reader03.plus.n et...[color=blue]
                  > You could also try
                  >
                  > Dim TempButtons() As New NavButtonInfo
                  >
                  > That seemed to work for me.
                  >
                  > -Jerry
                  >
                  > "Charles Law" <blank@nowhere. com> wrote in message
                  > news:%234xl1weW FHA.1796@TK2MSF TNGP15.phx.gbl. ..[color=green]
                  >> Hi Nathan
                  >>
                  >> Replace the line
                  >>[color=darkred]
                  >>> Dim TempButtons() As NavButtonInfo[/color]
                  >>
                  >> with
                  >>
                  >> Dim TempButtons(-1) As NavButtonInfo
                  >>
                  >> This will initialise the array with no elements.
                  >>
                  >> HTH
                  >>
                  >> Charles
                  >>
                  >>
                  >> "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
                  >> news:eaQ9WCdWFH A.3840@tk2msftn gp13.phx.gbl...[color=darkred]
                  >>>I have an Array that I will be resizing with the ReDim statement. For
                  >>>some reason that I cannot figure out, I am recieving the following error:
                  >>>
                  >>> [NullReferenceEx ception: Object reference not set to an instance of an
                  >>> object.]
                  >>>
                  >>> The code that this occurs in is as follows:
                  >>>
                  >>> Dim NavButton As NavButtonInfo
                  >>>
                  >>> Dim TempButtons() As NavButtonInfo
                  >>>
                  >>> For Each NavButton In ButtonList
                  >>>
                  >>> If NavButton.paren tname = tempparentname Then
                  >>>
                  >>> ReDim Preserve TempButtons(Tem pButtons.GetUpp erBound(0) + 1)
                  >>>
                  >>> TempButtons(Tem pButtons.GetUpp erBound(0)) = NavButton
                  >>>
                  >>> End If
                  >>>
                  >>> Next
                  >>>
                  >>>
                  >>> The error occurs on the ReDim line of this code. I am assuming that this
                  >>> has something to do with the fact that I initially declare TempButtons
                  >>> without giving it's size, but if I do not have the array the exact size
                  >>> (which is unknown until runtime) it will cause problems later in my
                  >>> code. For example, if I place constants like 10 and 5 in the Redim and
                  >>> following lines, it will get past this section of my code and then give
                  >>> me an error later in my code when it tries to read from indexes that
                  >>> have not been assigned values. Any help would be appreciated. Thanks.
                  >>> --
                  >>> Nathan Sokalski
                  >>> njsokalski@hotm ail.com
                  >>> http://www.nathansokalski.com/
                  >>>[/color]
                  >>
                  >>[/color]
                  >
                  >[/color]


                  Comment

                  • Nathan Sokalski

                    #10
                    Re: Problem with ReDim statement

                    This gives me the following error:



                    Followed by:

                    Server Application Unavailable
                    The web application you are attempting to access on this web server is currently unavailable. Please hit the "Refresh" button in your web browser to retry your request.

                    Administrator Note: An error message detailing the cause of this specific request failure can be found in the application event log of the web server. Please review this log entry to discover what caused this error to occur.

                    --
                    Nathan Sokalski
                    njsokalski@hotm ail.com
                    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


                    "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message news:%23i8l67hW FHA.3676@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                    > "Charles Law" <blank@nowhere. com> schrieb:[color=green]
                    >> If they are semantically equal, why do they behave differently?
                    >>
                    >> In the first case, the NullReferenceEx ception exception is thrown, in the
                    >> second case it is not.[/color]
                    >
                    > Sorry, my bad. I mixed the code up with these two lines which are
                    > semantically equivalent:
                    >
                    > \\\
                    > Dim b1() As Button = New Button() {}
                    > Dim b2() As Button = New Button(-1) {}
                    > ///
                    >
                    > --
                    > M S Herfried K. Wagner
                    > M V P <URL:http://dotnet.mvps.org/>
                    > V B <URL:http://classicvb.org/petition/>
                    >[/color]

                    Comment

                    • Charles Law

                      #11
                      Re: Problem with ReDim statement

                      Nathan

                      It wasn't apparent from your original post that the problem is in a web
                      application.
                      [color=blue]
                      > I don't know if there is some other modification I need to make along with
                      > the Dim TempButtons(-1) As
                      > NavButtonInfo, but it is obviously not fixing my problem.[/color]

                      I think it is fixing the problem that you originally reported, but it has
                      now exposed another problem. I will have to defer to someone better equipped
                      to help with diagnosing a web server problem, but you could review the log
                      entry, as the message suggests, as a start.

                      Good luck.

                      Charles


                      "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
                      news:OtIXzLnWFH A.3636@TK2MSFTN GP14.phx.gbl...
                      I have tried this, and it gives me the following errors:


                      After clicking OK, I recieve the following error:

                      The web application you are attempting to access on this web server is
                      currently unavailable. Please hit the "Refresh" button in your web browser
                      to retry your request.
                      Administrator Note: An error message detailing the cause of this specific
                      request failure can be found in the application event log of the web server.
                      Please review this log entry to discover what caused this error to occur.

                      I don't know if there is some other modification I need to make along with
                      the Dim TempButtons(-1) As NavButtonInfo, but it is obviously not fixing my
                      problem.
                      --
                      Nathan Sokalski
                      njsokalski@hotm ail.com
                      有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


                      "Charles Law" <blank@nowhere. com> wrote in message
                      news:%234xl1weW FHA.1796@TK2MSF TNGP15.phx.gbl. ..[color=blue]
                      > Hi Nathan
                      >
                      > Replace the line
                      >[color=green]
                      >> Dim TempButtons() As NavButtonInfo[/color]
                      >
                      > with
                      >
                      > Dim TempButtons(-1) As NavButtonInfo
                      >
                      > This will initialise the array with no elements.
                      >
                      > HTH
                      >
                      > Charles
                      >
                      >
                      > "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
                      > news:eaQ9WCdWFH A.3840@tk2msftn gp13.phx.gbl...[color=green]
                      >>I have an Array that I will be resizing with the ReDim statement. For some
                      >>reason that I cannot figure out, I am recieving the following error:
                      >>
                      >> [NullReferenceEx ception: Object reference not set to an instance of an
                      >> object.]
                      >>
                      >> The code that this occurs in is as follows:
                      >>
                      >> Dim NavButton As NavButtonInfo
                      >>
                      >> Dim TempButtons() As NavButtonInfo
                      >>
                      >> For Each NavButton In ButtonList
                      >>
                      >> If NavButton.paren tname = tempparentname Then
                      >>
                      >> ReDim Preserve TempButtons(Tem pButtons.GetUpp erBound(0) + 1)
                      >>
                      >> TempButtons(Tem pButtons.GetUpp erBound(0)) = NavButton
                      >>
                      >> End If
                      >>
                      >> Next
                      >>
                      >>
                      >> The error occurs on the ReDim line of this code. I am assuming that this
                      >> has something to do with the fact that I initially declare TempButtons
                      >> without giving it's size, but if I do not have the array the exact size
                      >> (which is unknown until runtime) it will cause problems later in my code.
                      >> For example, if I place constants like 10 and 5 in the Redim and
                      >> following
                      >> lines, it will get past this section of my code and then give me an error
                      >> later in my code when it tries to read from indexes that have not been
                      >> assigned values. Any help would be appreciated. Thanks.
                      >> --
                      >> Nathan Sokalski
                      >> njsokalski@hotm ail.com
                      >> http://www.nathansokalski.com/
                      >>[/color]
                      >
                      >[/color]

                      Comment

                      Working...