1

hit another problem during my C# practise. This is short explanation of it: In Program.cs I have the following code:

namespace testApp
{
  public class AppSettings
  {
    public static int appState { get; set; }
    public static bool[] stepsCompleted { get; set; }
  }

  public void Settings
  {
    appState = 0;
    bool[] stepsCompleted = new bool[]{false, false, false, false, false};
  }
}

static class MyApp
{
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new gameScreen());
    AppSettings appSettings = new AppSettings();
  }
}

And this is in the Form1.Designer.cs:

namespace testApp
{
  private void InitializeComponent() {..}
  private void detectPressedKey(object sender, KeyPressEventArgs e)
  {
    if (e.KeyChar == (char)13) // Enter = code 13
    {
      if (AppSettings.appState == 0)
      {
        if (AppSettings.stepsCompleted[1] == false) // << here we have an EXCEPTION!!!
        {
          this.playSound("warn");
        }
      }
    }
  }
}

The problem is in the commented if where I get NullReferenceException: Object reference not set to an instance of an object. Searched a bit through the net but cannot find where is the problem. AppSettings.stepsCompleted should exist like AppSettings.appState

1
  • 1
    You are not assigning anything non-null to AppSettings.stepsCompleted, so it will be null. It does exist, but it doesn't have a value, so attempting to access its elements will throw a NRE. Commented May 27, 2015 at 4:36

1 Answer 1

5

You aren't initializing the AppSettings.stepsCompleted anywhere. In fact, testApp.Settings won't compile. Since your AppSettings class has static members, which you access from your form, and assuming you need only a single instance to track state, what you can do is initialize them via a static constructor:

public static class AppSettings // May as well make the class static 
{
  public static int appState { get; set; }
  public static bool[] stepsCompleted { get; set; }

  static AppSettings() // Static constructor
  {
    appState = 0;
    stepsCompleted = new []{false, false, false, false, false};
  } 
}

You then need to remove the line from Main:

AppSettings appSettings = new AppSettings();

Static constructors are guaranteed to be invoked once, prior to the first access

Edit - Full working sample

Program.cs

using System;
using System.Windows.Forms;

namespace testApp
{
    public static class AppSettings // May as well make the class static 
    {
        public static int appState { get; set; }
        public static bool[] stepsCompleted { get; set; }

        static AppSettings() // Static constructor
        {
            appState = 0;
            stepsCompleted = new[] { false, false, false, false, false };
        }
    }

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new gameScreen());
        }
    }
}

Form1.cs (gameScreen)

using System.Windows.Forms;

namespace testApp
{
    public partial class gameScreen : Form
    {
        public gameScreen()
        {
            InitializeComponent();
        }

        private void gameScreen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13) // Enter = code 13
            {
                if (AppSettings.appState == 0)
                {
                    if (AppSettings.stepsCompleted[1] == false)
                    {
                        this.playSound("warn");
                    }
                }
            }
        }

        private void playSound(string someSound)
        {
            MessageBox.Show(string.Format("Sound : {0}", someSound));
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Well, did your suggestions and I've got the same exception
... but realized that I should remove bool[] in front of stepsCompleted in the constructor, in fact I missed it :). Now it works, answer approved! Btw Im'm using embedded resource for playSound().
Apols - I didn't notice that change - new []{false, false, false, false, false} and new bool[]{false, false, false, false, false} should both work - the compiler can infer that its an array of booleans. My Resharper cleanup is set to remove anything it deems redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.