静态成员未初始化- get系统.来自数组元素的NullReferenceException

本文关键字:数组元素 NullReferenceException 系统 初始化 get 静态成员 | 更新日期: 2023-09-27 17:50:57

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

这是在form。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一样存在

静态成员未初始化- get系统.来自数组元素的NullReferenceException

您没有在任何地方初始化AppSettings.stepsCompleted。事实上,testApp.Settings不会编译。由于AppSettings类具有静态成员,您可以从表单访问这些成员,并且假设您只需要一个实例来跟踪状态,您可以做的是通过静态构造函数初始化它们:

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