XNA 4.0. Null reference exception -


**public graphicsdevicemanager graphics;         public spritebatch spritebatch;         private int[] heights;         //public texture2d terrain;         public texture2d[] terrainlist;         public color[] colors_terraintexture;         public int [] coordinates_x;         camera camera = new camera();         graphicsdevice device;          public mapfunctions()                         {          }     public void generatemap_heights(int map_width,int map_height,float relief,float mountain_peak_height)             {                 heights = new int[map_width];                 random randomizer = new random();                 double rand1 = randomizer.nextdouble() + 1;                 double rand2 = randomizer.nextdouble() + 2;                 double rand3 = randomizer.nextdouble() + 3;                  float offset = map_height / 2;                 float peakheight = 324;                 float flatness = mountain_peak_height;                  (int x = 0; x < map_width; x++)                 {                     double height = peakheight / rand1 * math.sin((float)x / flatness * rand1 + rand1);                     height += peakheight / rand2 * math.sin((float)x / flatness * rand2 + rand2);                     height += peakheight / rand3 * math.sin((float)x / flatness * rand3 + rand3);                     height += offset;                     heights[x] = (int)height;                 }             }              public bool candraw = false;              public void createterrain(int map_width, int map_height,int terraintexture_width,color color_terrain,color color_background)             {                             int timestoloop = (map_width / terraintexture_width) + 1;                 int[] coordinates_x = new int[timestoloop];                 device = graphics.graphicsdevice;                 (int b = 0; b <= timestoloop; b++)                 {                     if (b == timestoloop)                     {                         colors_terraintexture = new color[map_width % terraintexture_width * map_height];                         terraintexture_width = map_width % terraintexture_width;                     }                     else                      {                         colors_terraintexture = new color[terraintexture_width * map_height];                     }                     coordinates_x[b] = terraintexture_width * b;                     (int x = 0; x < terraintexture_width; x++)                     {                                             (int y = 0; y < map_height; y++)                         {                             if (y > heights[x])                                 colors_terraintexture[x + y * terraintexture_width] = color.green;                             else                                 colors_terraintexture[x + y * terraintexture_width] = color.transparent;                         }                     }                                     terrain = new texture2d(device, terraintexture_width, map_height, false, surfaceformat.color);                     terrain.setdata(colors_terraintexture);                     terrainlist[b] = terrain; //i nullreference exception in line                 }                 spritebatch = new spritebatch(device);                 candraw = true;             }** 

terrainlist[b] = terrain; (this line in end of code) null reference exception in line. call both functions (createterrain , generatemap_heights)from game1 loadcontent(). please help, can't find problem in code.

sorry bad english.

do ever create terrainlist? looking through code , did not see it.

texture2d[] terrainlist = new texture2d[10]; 

right trying set 1 of texture2d's in array, hasn't been created yet.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -