【问题标题】:C# 'Object reference not set' error when working with lists within a class [duplicate]使用类中的列表时出现 C#“未设置对象引用”错误 [重复]
【发布时间】:2020-07-26 05:04:19
【问题描述】:

这对你的专业人士来说非常容易 - 但我现在自己在这上面浪费了好几个小时。这是我正在尝试做的简化版本:

(1) 我有一个“Employment”类,它可以存储 3 个字符串 [EmployerName]、[JobTitle] 和 [PeriodOfEmployment]。我成功地创建了一个就业实例并用示例条目填充它。

(2) 我想填充一个名为“CurriculumVitae”的包装类,其中包含一个 [PersonName] 字符串和一个包含该人曾经持有的所有就业的 List 变量。我想使用循环将就业实例添加到 CurriculumVitae 实例中,以便 CurriculumVitae 类最终在其就业列表中保存一个人的完整工作历史。

(3) 在 Visual Studio 中收到错误消息:

System.NullReferenceException: 'Object reference not set to an instance of an object.'
CurriculumVitae.EmploymentsList.get returned null.'.

我的简化代码是:

using System;
using System.Collections.Generic;

namespace CurriculumVitaeExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var CurriculumVitae = new CurriculumVitae();
            CurriculumVitae.Open();
        }
    }
}

我的就业课程如下所示:

public class Employment
{
    public string EmployerName { get; set; }
    public string JobTitle { get; set; }
    public string PeriodOfEmployment { get; set; }
}

我的 CurriculumVitae 课程尝试使用 List 中的就业课程,如下所示:

public class CurriculumVitae //(version 1)
{
    public string PersonName { get; set; }
    public List<Employment> EmploymentsList { get; set; }

    // Open method:
    public void Open()
    {
        Employment Employment = new Employment();

        Employment.EmployerName = "McDonalds";
        Employment.JobTitle = "Ice Cream Guru";
        Employment.PeriodOfEmployment = "Jan 2019 - Present";

        this.EmploymentsList.Add(Employment);
    }

}

我还尝试在 CurriculumVitae 类中为就业列表添加一个构造函数,但没有帮助:

public class CurriculumVitae //(version 2)
{
        // Constructor:
        public CurriculumVitae()
        {
            List<Employment> EmploymentsList = new List<Employment>();
        }
        ...
}

【问题讨论】:

  • 上线出现错误:this.EmploymentsList.Add(Employment);
  • 您需要了解局部变量与属性的区别以及如何实例化对象列表。在 CurriculumVitae 构造函数中,您只需要 EmploymentsList = new List&lt;Employment&gt;(); 您所拥有的内容会创建一个新的局部变量,但这无济于事。

标签: c# list class constructor object-oriented-analysis


【解决方案1】:

CurriculumVitae变量名改为curriculumVitae

static void Main(string[] args)
{
    var curriculumVitae = new CurriculumVitae();
    curriculumVitae.Open();
}

Employmentemployment

public void Open()
{
    Employment employment = new Employment();
    employment.EmployerName = "McDonalds";
    employment.JobTitle = "Ice Cream Guru";
    employment.PeriodOfEmployment = "Jan 2019 - Present";

    this.EmploymentsList.Add(employment);
}

最后你必须像这样在version1中初始化EmploymentsList

public List<Employment> EmploymentsList { get; set; } = new List<Employment>();

constructorversion2

public CurriculumVitae()
{
    this.EmploymentsList = new List<Employment>();
}

【讨论】:

  • 您是否阅读了问题中的错误信息?您的答案如何解决该错误? (提示:它没有)
  • @mason 类名和变量名相同。和CurriculumVitae.Open(); 不起作用,因为Open() 方法不是静态的,应该创建CurriculumVitae 的实例
  • 感谢您的回复 - 我确实看到它与空引用有关,但作为初学者,我无法将这种见解转化为对 this.EmploymentList.Add(employment) 行的简单修复;我希望这是一个专业人士可以发现的简单语法错误。
  • 再次重申:您的解决方案 nothing 来解决问题中的错误消息。你读过这个问题吗?
  • @JBrog 支持和反对控制也在那里。在 cmets 中的用户名前使用“@”符号来通知该人您正在对他们发表评论。
猜你喜欢
  • 1970-01-01
  • 2013-07-07
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 2023-01-30
相关资源
最近更新 更多