【问题标题】:Object reference not set to an instance of an object when referencing a settings object [duplicate]引用设置对象时,对象引用未设置为对象的实例[重复]
【发布时间】:2014-05-08 20:52:45
【问题描述】:

我的 Properties.Settings.Default 中有一个设置属性,它是“string[][]”(由字符串数组构建的字符串数组)。 我是这样设置的,看起来VS读取正确:

<Setting Name="Markers" Type="System.String[][]" Scope="User">

这是我的代码:

string[][] buttons = (string[][])Properties.Settings.Default.Markers;
buttons[0] = new string[] { "pause", "", "0", "255", "0" }; // Object reference not set to an instance of an object - here

buttons[1] = new string[] { "pause", "", "0", "255", "0" };

buttons[2] = new string[] { "effect", "", "255", "0", "0" };

buttons[3] = new string[] { "interest", "", "255", "255", "0" };

buttons[4] = new string[] { "cut", "", "0", "255", "255" };

其余代码与 Properties.Settings.Default.Markers 无关。我刚开始处理这个

编辑: 我发现按钮是空的......我发现初始化它的唯一方法是:Properties.Settings.Default.Markers = new string[100][];问题是它最多有 100 个数组。我的问题是有没有办法在不设置长度的情况下对其进行初始化?

【问题讨论】:

  • 这不是一个二维数组,你有一个锯齿状数组。
  • 是的...我用谷歌搜索了一下,这似乎就是我的意思,一个数组数组...
  • 您是否使用调试器查看了第一行代码中按钮变量的设置?作为唯一可能真正导致第二行错误的是buttons == null。
  • 所以您的按钮对象为空?按钮具有 string[][] 类型,但我没有看到它是从您的示例中初始化的。
  • NullReferenceException 的几乎所有情况都是相同的。请参阅“What is a NullReferenceException in .NET?”获取一些提示。

标签: c# .net arrays settings nullreferenceexception


【解决方案1】:

我继续搜索了很多,发现了这个:How to store a list of objects in application settings

这回答了我的问题并帮助了我很多

【讨论】:

    猜你喜欢
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多