Jancis
Jancis

Reputation: 97

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;

                for (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;
                for (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;
                    for (int x = 0; x < TerrainTexture_Width; x++)
                    {                    
                        for (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 get nullreference exception in this line
                }
                spriteBatch = new SpriteBatch(device);
                CanDraw = true;
            }**

TerrainList[b] = Terrain; (this line is almost in end of the code) I get null reference exception in this line. I call both functions (CreateTerrain and GenerateMap_Heights)from game1 LoadContent(). Please help, I can't find problem in my code.

Sorry for my bad english.

Upvotes: 0

Views: 100

Answers (3)

phadaphunk
phadaphunk

Reputation: 13303

You create you terrain list but never initialize it ...

//Something like
TerrainList = new Texture2D[];

Now you try to access an index of a null collection.

Upvotes: 1

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

TerrainList is never initialized in the provided code, so when you index it with TerrainList[b] this returns null. You need to create an instance of Texture2D[] and assign it to TerrainList. Furtermore, you need to make sure it has at least b number of elements, since you're using b to index the array.

Upvotes: 0

Scott
Scott

Reputation: 93

Do you ever create your TerrainList? I was looking through the code and did not see it.

Texture2D[] TerrainList = new Texture2D[10];

Right now you are trying to set one of the Texture2D's in your array, but it hasn't been created yet.

Upvotes: 0

Related Questions