gpt4 book ai didi

c# - 静态成员未初始化 - 从数组元素中获取 System.NullReferenceException

转载 作者:太空宇宙 更新时间:2023-11-03 19:59:01 24 4
gpt4 key购买 nike

在我的 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(游戏画面)

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com