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 6574467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:15:56+00:00 2026-05-25T15:15:56+00:00

So, I have a list of vectors, when I put a new vector in

  • 0

So, I have a list of vectors, when I put a new vector in it it works perfectly, but If I try to access an index of List of Vectors and put a vector in it, I get this error “Object reference not set to an instance of an object.” The code is almost the same for each List:

class GameMap
{
    MouseHandler mouseHandler;
    TileSet tileSet;
    List<Vector2>[] tiles;
    List<Vector2> tile;

    public GameMap(ContentManager Content)
    {
        mouseHandler = new MouseHandler();
        tileSet = new TileSet(Content);
    }

    public void Initialize()
    {
        tiles = new List<Vector2>[tileSet.tiles]; //What am I doing wrong here?
        tile = new List<Vector2>();
    }

    public void MapEditor()
    {
        mouseHandler.MouseUpdate();

        if (mouseHandler.LeftButton == true)
        {
            tiles[0].Add(mouseHandler.MousePosition); //The error comes out here.
            tile.Add(mouseHandler.MousePosition);
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int i = 0; i < tileSet.tiles; i++)
        {
            tileSet.TiledTexture(spriteBatch, tiles[i], i);
            tileSet.TiledTexture(spriteBatch, tile, i);
        }
    }
}

The “Tiles” with multiples Lists of vectors isn’t working, what am I doing wrong?

  • 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-05-25T15:15:56+00:00Added an answer on May 25, 2026 at 3:15 pm
    tiles = new List<Vector2>[tileSet.tiles]
    

    creates a new array of List<Vector2> with tileSet.tiles elements. Those elements are null initially. When you access tiles[0].Add(...), tiles[0] is still null, and thus you get a null reference exception.

    You need to do something similar to this:

    tiles = new List<Vector2>[tileSet.tiles]
    for (int i = 0; i < tiles.Length; ++i)
    {
         tiles[i] = new List<Vector2>();
    }
    

    EDIT:

    I guess the point is this:

    Although

    tile = new List<Vector2>()
    

    and

    tiles = new List<Vector2>[tileSet.tiles]
    

    look very similar, they are quite different. The first creates a new List, and the second creates an array of List references, which are initially null. You still have to initialize each of those references to a valid list before you can use it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The name says it all, but to elaborate I have a list of vectors
I have a vector of Linked list pointers. Each LinkedList has a head pointer
Say you have a simple class with some storage data structure (list, vector, queue,
I have a list of vectors - the set of types is known and
I am trying to get a vector to store objects of class 'Complex'. This
I have this code which is giving the error: error this boost::lock_error error this
I have a list of character vectors in R that represents sets of cooccuring
I have a list of character strings (vectors) and I have a list of
I have a vector<CustomClass*> and I put a lot of items in the vector
I have a list of vectors (in Python) that I want to normalize, while

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.