what is this error message?

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

    #1

    what is this error message?

    Hello

    Does anyone know what this message could be regarding?
    The application seems to fall over on the following line.
    Dim variable1 As SqlString = Arraylist1(i

    The first time the code runs everything is ok. The problem seems to occur when the application is run through a second time

    here is the error message im getting....

    An unhandled exception of type 'System.Argumen tOutOfRangeExce ption' occurred in mscorlib.dl
    Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
  • Jon Skeet [C# MVP]

    #2
    Re: what is this error message?

    Bhavna <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
    > Does anyone know what this message could be regarding??
    > The application seems to fall over on the following line..
    > Dim variable1 As SqlString = Arraylist1(i)
    >
    > The first time the code runs everything is ok. The problem seems to
    > occur when the application is run through a second time.
    >
    > here is the error message im getting.....
    >
    > An unhandled exception of type 'System.Argumen tOutOfRangeExce ption'
    > occurred in mscorlib.dll
    > Additional information: Index was out of range. Must be non-negative
    > and less than the size of the collection.[/color]

    Well, the error message is pretty clear: the index you're using is out
    of range. In other words, i is either too large or too small. It must
    be at least 0 and also less than the number of elements in the
    collection. For instance, if your collection has 5 elements, the index
    you use can only be in the range 0-4.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Cor

      #3
      Re: what is this error message?

      Hi Bhavna,

      In addition to Jon, this message can also come when you have cleared your
      arraylist before with something as Arraylist.clear

      I hope this helps,

      Cor


      Comment

      • William Ryan  eMVP

        #4
        Re: what is this error message?

        Hi Bhavna:
        "Bhavna" <anonymous@disc ussions.microso ft.com> wrote in message
        news:0EB8635D-4528-47B0-AB22-CDFAD3873A77@mi crosoft.com...[color=blue]
        > Hello,
        >
        > Does anyone know what this message could be regarding??
        > The application seems to fall over on the following line..
        > Dim variable1 As SqlString = Arraylist1(i)
        >
        > The first time the code runs everything is ok. The problem seems to occur[/color]
        when the application is run through a second time.[color=blue]
        >
        > here is the error message im getting.....
        >
        > An unhandled exception of type 'System.Argumen tOutOfRangeExce ption'[/color]
        occurred in mscorlib.dll[color=blue]
        > Additional information: Index was out of range. Must be non-negative and[/color]
        less than the size of the collection.


        For some reason, on the second pass, something different must be happening.
        Like Jon mentions, this is a pretty specific error and I'd recommend a few
        things....

        Every method doesn't need to have a try catch and depending on the
        circumstances, you don't always want code wrapped in try/catch clauses.
        However, Something should probably catch the exception somewhere up the
        chain. In this instance, you aren't doing any bounds checking before you
        make the reference which invites such problems. granted it's fairly
        straightforward to prevent index out of range problems by really
        understanding your code, but you should probably verify that you have a good
        index before you reference it in the spirit of defensive programming.

        Anyway, try this.....Before you call the statement which is throwing the
        exception,
        Debug.Assert(i <= ArrayList.Count-1) or something like If (i <=
        ArrayList.Count AND i > -1) Then
        Dim variable1 As SqlString = Arraylist1(i)
        End If

        It really depends on the whole program and coding strategy. If you opt for
        a check before the reference, then you know you'll have a valid index. on
        the other hand, you can check everythign under the sun which can be overkill
        in situations where you can be quite sure that you have a valid index to
        begin with. In such cases, using Assertions liberally will alert you of any
        problems before you throw the exception.

        There are a lot of things that could be happening on the second pass that
        didn't happen the first time which could be causing this. It'd be impossible
        to tell based on that code snippet what it could be, but check the Count of
        the Array List and it will probably tip you off to the problem. If not, I'd
        starting asserting preconditions throughout my code just to be sure that
        everything I'm assuming to be true is a correct assumption.

        HTH,

        Bill



        Comment

        • Bhavna

          #5
          Re: what is this error message?

          Hi Cor and Jon

          Thx for both of your inputs. They have both been very useful.
          I will have to look through my code now to see what is goin on with the arraylis

          Many thx

          Comment

          Working...