运行时空引用异常

时间:2014-02-23 15:12:23

标签: c# nullreferenceexception

我在运行以下代码时得到空引用异常。我创建了一个返回对象集合的类。在主类中打印值。

//main class where i am calling employee class.
class Program
    {
            static void Main(string[] args)
            {
                //Creating an object for employee class.
                Employees obje = new Employees();
                var obj2 = obje.EmployeeDetails();

                //looping through each object and printing the values.
                foreach (var emp in obj2)
                {
                    Console.WriteLine(emp.Employeeid);
                    Console.WriteLine(emp.Employeename);
                    Console.WriteLine(emp.Employeeage);
                }
                Console.ReadKey();
            }
        }


    //Employee class with some properties and function. 
     class Employees
        {
            public int Employeeid { get; set; }
            public string Employeename { get; set; }
            public string Employeeage { get; set; }


            public Employees[] EmployeeDetails()
            {
                Employees[] objEmployees = new Employees[2];

                objEmployees[0].Employeeage = "20";
                objEmployees[0].Employeeid = 101;
                objEmployees[0].Employeename = "Arjunan";

                objEmployees[1].Employeeage = "24";
                objEmployees[1].Employeeid = 102;
                objEmployees[1].Employeename = "Shiva";

                return objEmployees;
            }
        }

请告诉我我在哪里做错了。新手到dotnet世界。

8 个答案:

答案 0 :(得分:2)

首先,您需要使用new关键字创建数组元素。

 objEmployees[0] = new Employees();
 objEmployees[0].Employeeage = "20";

答案 1 :(得分:1)

您的Employeesclass。这就是为什么它是引用类型。如果您创建一个带引用类型的数组,则所有数组项都默认为null

这意味着您的objEmployees[0]null作为默认设置,并尝试设置null的属性。这就是你得到NullReferanceException的原因。

来自Arrays (C# Programming Guide)

  

数值数组元素的默认值设置为零,和   引用元素设置为null。

作为解决方案,您可能需要为数组项创建一个新的Employees对象(带有new关键字),或者您可以使用数组初始值设定项,例如 tvanfosson's answer < / em>的

答案 2 :(得分:0)

员工[] objEmployees =新员工[2]; //这会创建数组而不是其中的项目。

答案 3 :(得分:0)

这样做:

    public Employees[] EmployeeDetails()
    {
        Employees[] objEmployees = new Employees[2];
        objEmployees[0] = new Employees();//Note to the new instance
        objEmployees[0].Employeeage = "20";
        objEmployees[0].Employeeid = 101;
        objEmployees[0].Employeename = "Arjunan";

        objEmployees[1] = new Employees();//Note to the new instance
        objEmployees[1].Employeeage = "24";
        objEmployees[1].Employeeid = 102;
        objEmployees[1].Employeename = "Shiva";

        return objEmployees;
    }

在访问它们之前,您必须使用新对象(Employees)实例化数组项,否则它将等于null引用。

答案 4 :(得分:0)

在设置值之前,您需要在数组中实例化Employees类:

        public Employees[] EmployeeDetails()
    {
        Employees[] objEmployees = new Employees[2];

        objEmployees[0] = new Employees();
        objEmployees[0].Employeeage = "20";
        objEmployees[0].Employeeid = 101;
        objEmployees[0].Employeename = "Arjunan";

        objEmployees[1] = new Employees();
        objEmployees[1].Employeeage = "24";
        objEmployees[1].Employeeid = 102;
        objEmployees[1].Employeename = "Shiva";

        return objEmployees;
    }

答案 5 :(得分:0)

 public Employees[] EmployeeDetails()
            {
                Employees[] objEmployees = new Employees[2];

                objEmployees[0].Employeeage = "20";
                objEmployees[0].Employeeid = 101;
                objEmployees[0].Employeename = "Arjunan";

                objEmployees[1].Employeeage = "24";
                objEmployees[1].Employeeid = 102;
                objEmployees[1].Employeename = "Shiva";

                return objEmployees;
            }

此方法导致问题。当您创建Employees数组时,您所做的只是分配内存。您实际上没有创建任何Employees个对象并将它们放在数组中。

因此,当您尝试执行objEmployees[0].Employeeage = "20"时,您将获得NullReference异常,因为Employees处不存在objEmployees[0]对象。

答案 6 :(得分:0)

问题是您正在创建数组,但在尝试使用它们之前,您不会在数组中创建对象。在这种情况下,处理它的更好方法是使用数组初始化器。

 return new []
 {
     new Employee
     {
         EmployeeAge = 20,
         EmployeeId = 101,
         EmployeeName = "Arjunan",
     },
     new Employee
     {
         EmployeeAge = 24,
         EmployeeId = 102,
         EmployeeName = "Shiva",
     },
 };

注意我已经使属性的名称遵循PascalCase并假设age属性确实应该是数字,而不是字符串。

答案 7 :(得分:0)

试试这个

 public Employees[] EmployeeDetails()
    {
        var objEmployees = new Employees[2];
        objEmployees[0] = new Employees();
        objEmployees[0].Employeeage = "20";
        objEmployees[0].Employeeid = 101;
        objEmployees[0].Employeename = "Arjunan";

        objEmployees[1] = new Employees();
        objEmployees[1].Employeeage = "24";
        objEmployees[1].Employeeid = 102;
        objEmployees[1].Employeename = "Shiva";



        return objEmployees;

    }

您需要为每个objEmployees

创建一个实例