Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7838307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:03:56+00:00 2026-06-02T15:03:56+00:00

Can anyone tell me why I am getting this exception? Well I know why

  • 0

Can anyone tell me why I am getting this exception? Well I know “why” I am getting it is cause it says that the element in the list I am trying to access does not exist, but stepping through the code I can see that it actually does exist?

In Game1 I call a helper class’ calculation method. Then when the calculation is done, I add the result to a List<> in the CalculationHelper class.
The code for this looks like:

In Game1 class where I have instanciated CalculationHelper calcHelper class and then call one of the class methods.
Fisrt, in Game1 I call calcHelper.ForceVanishingPointIntersections(…) method in a for() loop which can be seen below. This works just fine and the calcHelper.ForceVanishingPointIntersections(…) method adds the value to the IntersectionPointList<> in the CalculationHelper class (see method below).

for (int i = 0; i < LineList.Count; i++)
{
    calcHelper.ForceVanishingPointIntersections(LineList[i], LineList[i + 1]);

    AddSprite(VanishingPointIntersectionList, calcHelper.GetIntersectionPointListElementAt(i), greenCirlce);
    i++;
}

To do some calculations and add a value to the IntersectionPointlist in CalculationHelper class I do:

List<Vector2> IntersectionPointList = new List<Vector2>();
public void ForceVanishingPointIntersections(Line line1, Line line2)
{
    Vector2 intersectionPoint;
    // Do some calculations

    IntersectionPointList.Add(intersectionPoint);
}

Finally in Game1 class, the second method in the for() loop I call AddSprite to create a Sprite. I want to pass in the values stored in CalculationHelper class IntersectionPointList as coordinates for the Sprite.
To this end I call calcHelper.GetIntersectionPointListElementAt(i) which calls a method in CalculationHelper class like so (which should return the value at the specified point (i) from the IntersectionPointList<>)
public Vector2 GetIntersectionPointListElementAt(int index)
{
Vector2 result = this.IntersectionPointList[index];
return result;
}
The first time the for() loop executes, this works fine. I do the calculations, the value is stored in the list and I am able to get this value from the list and pass it to AddSprite(..). However, the second time round the for() loop, when I call GetIntersectionPointListElementAt from the AddSprite() method, I get an exception in my

public Vector2 GetIntersectionPointListElementAt(int index)
{
    Vector2 result = this.IntersectionPointList[index];    // Exception thrown here
    return result;
}

saying the index was out of range? But stepping through the code, my IntersectionPointList is updated and it shows that the list now contains 2 values. And I’m trying to access the second value.

Does anyone have an idea why this could be?
For the life of me I cant figure out where I am going wrong.

If more code is needed I can post some more, just let me know

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:03:58+00:00Added an answer on June 2, 2026 at 3:03 pm

    Because you access LineList[] with an index i + 1 you must diminish the last index by one in the for-condition. (Note the -1)

    for (int i = 0; i < LineList.Count - 1; i++) {
        calcHelper.ForceVanishingPointIntersections(LineList[i], LineList[i + 1]);
        AddSprite( ... );
    }
    

    This will call ForceVanishingPointIntersections with the indexes

    (0, 1), (1, 2), (2, 3), ... (Count-2, Count-1)
    

    Note that the index range of LineList is 0 ... Count-1.


    If non-overlapping indexes are required instead, like

    (0, 1), (2, 3), (4, 5), ... (Count-2, Count-1)
    

    then change the loop to

    for (int i = 0; i < LineList.Count - 1; i += 2) {
        calcHelper.ForceVanishingPointIntersections(LineList[i], LineList[i + 1]);
        AddSprite(
            VanishingPointIntersectionList,
            calcHelper.GetIntersectionPointListElementAt(i / 2),
            greenCirlce);
    }
    

    EDIT According the Chris Gessler’s comments this second approach is the right one.

    Remove the i++ inside the loop, it’s quite uncommon and confusing. Instead, replace it by i += 2 in loop header.

    Also note that (again according to Chris Gessler) IntersectionPointList has half as much items as LineList. Therefore call GetIntersectionPointListElementAt with i / 2. Since i is {0, 2, 4, ... }, i / 2 is {0, 1, 2, ...}.


    The for-loop allows you to have a comma-separated list of statements in the first and third section. You could use it to maintain two indexes

    for (int i = 0, k = 0; i < LineList.Count - 1; i += 2, k++) {
        calcHelper.ForceVanishingPointIntersections(LineList[i], LineList[i + 1]);
        AddSprite(
            VanishingPointIntersectionList,
            calcHelper.GetIntersectionPointListElementAt(k),
            greenCirlce);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting this error. Can anyone tell me what it means or how
Can anyone tell me what's wrong with this code? class Dataset < ActiveRecord::Base has_many
Can anyone tell me what is wrong with this code block? The PHP compiler
Can anyone tell me whats wrong in this code? when i try this i
Can anyone tell me why am i getting error message java.lang.NullPointerException in following code??
Can anyone tell me whether Helvetica is a browser base font? If so, it
Can anyone tell me why ActionScript 3, a statically typed language, doesn't have generics?
Can anyone tell me how to write and run msbuild by using VS 2005?
Can anyone tell me how to do runtime debugging on shared libraries? I need
can anyone tell me what is the best way to do something like, from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.