在我的C#实践中遇到了另一个问题。这是它的简短说明:
在Program.cs中,我有以下代码:

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();
  }
}


这是在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");
        }
      }
    }
  }
}


问题出在我得到if的带注释的NullReferenceException: Object reference not set to an instance of an object中。在网上搜索了一下,但找不到问题所在。 AppSettings.stepsCompleted应该像AppSettings.appState一样存在

最佳答案

您没有在任何地方初始化AppSettings.stepsCompleted。实际上,testApp.Settings无法编译。由于您的AppSettings类具有静态成员,您可以从表单中访问它们,并假设只需要一个实例来跟踪状态,因此您可以通过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};
  }
}


然后,您需要从Main中删除​​该行:

AppSettings appSettings = new AppSettings();


保证静态构造函数在第一次访问之前被调用一次

编辑-完整的工作样本

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));
        }
    }
}

关于c# - 静态成员未初始化-从数组元素获取System.NullReferenceException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30473234/

10-17 02:00