Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

When i run this code, it gives me "Object reference not set to an instance of an object." here

clickableArea.Add(temp);

here is the entire class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ScreenManager
{
class Menu
{
    public List<string> MenuItems;
    private List<Vector2> Position;
    private int iterator;
    public string InfoText { get; set; }
    public string Title { get; set; }
    public List<Rectangle> clickableArea;


    public int Iterator
    {
        get
        {
            return iterator;
        }
        set
        {
            iterator = value;
            if (iterator > MenuItems.Count - 1) iterator = MenuItems.Count - 1;
            if (iterator < 0) iterator = 0;
        }
    }

    public Menu()
    {
        Title = "Title";
        MenuItems = new List<string>();
        MenuItems.Add("New Game");
        MenuItems.Add("Options");
        MenuItems.Add("Exit Game");
        Iterator = 0;
        InfoText = string.Empty;
    }



    public int GetNumberOfOptions()
    {
        return MenuItems.Count;
    }

    public string GetItem(int index)
    {
        return MenuItems[index];
    }

    public void DrawMenu(SpriteBatch batch, int screenWidth, SpriteFont arial)
    {
        batch.Begin();

        Vector2 FontOrigin = arial.MeasureString(Title) / 2;

        batch.DrawString(arial, Title, new Vector2(screenWidth / 2 - arial.MeasureString(Title).X / 2, 20), Color.LightGreen, 0, FontOrigin, 1.0f, SpriteEffects.None, 0.5f);

        int yPos = 100;
        for (int i = 0; i < GetNumberOfOptions(); i++)
        {
            Rectangle temp;

            Color colour = Color.White;
            if (i == Iterator)
            {
                colour = Color.Gray;
            }



            batch.DrawString(arial, GetItem(i), new Vector2(screenWidth / 2 - arial.MeasureString(GetItem(i)).X / 2, yPos), colour);

            int reclengthX = (int)arial.MeasureString(MenuItems[i]).X;
            int reclengthY = (int)arial.MeasureString(MenuItems[i]).Y;

            int recposX = (int)(screenWidth / 2 - arial.MeasureString(MenuItems[i]).X / 2);

            temp = new Rectangle(reclengthX, reclengthY, recposX, yPos);

            clickableArea.Add(temp);

            yPos += 50;
        }
        batch.End();
    }

    public void DrawEndScreen(SpriteBatch batch, int screenWidth, SpriteFont arial)
    {
        batch.DrawString(arial, InfoText, new Vector2(screenWidth / 2 - arial.MeasureString(InfoText).X / 2, 300), Color.White);
        string prompt = "Press Enter to Continue";
        batch.DrawString(arial, prompt, new Vector2(screenWidth / 2 - arial.MeasureString(prompt).X / 2, 400), Color.White);
    }

}


}
share|improve this question

closed as too localized by Byte56, Sean Middleditch, Trevor Powell, Nathan Reed, Nick Wiggill Dec 30 '12 at 20:39

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Voting to close as too localized. As problems like this usually are. –  Byte56 Dec 29 '12 at 22:42
    
HI slefi, you should take a look into using the debugger in your IDE. Stepping through code and inspecting values will help you identify the problem. GDSE considers it off topic to ask us to debug your code for you. –  Sean Middleditch Dec 30 '12 at 5:24

1 Answer 1

clickableArea = new List<Rectangle>();

...needs to come first. Your list is not yet instantiated, so you cannot add anything to it. All you have there at the moment is an empty reference.

share|improve this answer
    
clickableArea = new List<Rectangle>(); to be exact. The constructor is a good place to instantiate the list. –  dadoo Games Dec 29 '12 at 22:28
    
oh wow, i cannot belive i did not see that, thanks –  slefi Dec 29 '12 at 22:34
    
@dadooGames Thanks corrected. Java on one screen, C# on the other. –  Nick Wiggill Dec 29 '12 at 22:34
    
@slefi Remember to hit the checkmark beside my answer, if this solved your problem. –  Nick Wiggill Dec 29 '12 at 22:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.