multi dimensional array of lists

I am trying to create a multidimensional array of lists that stores what is on a specific tile on my grid
basically the end goal is to get here
item = map_data.mapdata[x, y];
from what i can gather from other forms posts it should look like this
public class mapdata
{

  • public List[,] map_Data;*
  • public mapdata(int sizex, int sizey)*
  • {*
  •  map_Data = new List<int>[sizex, sizey];* 
    
  • }*
    }

public mapdata map_data = new mapdata(size_x,size_y);

public DTileMap(int size_x, int size_y)

{

  •  for(int x=0;x<size_x;x++) {*
    
  •  	for(int y=0;y<size_y;y++) {*
    
  •  		map_data.map_Data[x,y].Add(3);*
    
  •  	}*
    
  •  }*
    

}
but it tells me that
NullReferenceException: Object reference not set to an instance of an object
DTileMap…ctor (Int32 size_x, Int32 size_y) (at Assets/TileMap_D/DTileMap.cs:107)

I’m not sure, that it’s rigth declare:

 public List<int>[,] map_Data;

But if works, than you create multi list, but not initialization it. Try:

 public mapdata(int sizex, int sizey) {
  map_Data = new List<int>[sizex, sizey];
  //Initialization each list
  for(int i = 0; i < sizex; i++) {
   for(int j = 0; j < sizey; j++) {
    map_Data[i, j] = new List<int>();
   }
  }
 }

I hope that it will help you.