虽然我实例化了对象,但对象引用未设置为对象错误的实例

时间:2011-04-05 18:00:54

标签: c#

  public partial class College
  {
    public CollegeDetails CollegeDetails;
    public List<Students> Students;
    public StaffDetails StaffDetails;
  }

 public partial class Students 
 {
    public StudentDetails StudentDetails ;
    public List<Marks> Marks;
 }

在我的aspx.cs文件中

        College college = new College ();
    Students students= new Students (); 
    //List<Students> students = new List<Students>; 
     if (IsValidPost())
            {
                if (Session["xml"] == null)
                {
                    college.students.Add(new students{ });
                    Addtogrid();
                }
                else
                {
                    college = (College)(Session["xml"]);

                    if (college.students.Count == 0)----getting object refernce erro here
                    {
                        college.students.Add(new students{ });
                    }

                    Addtogrid();
                 }
            }
            else
            {
                if (Session["xml"] != null)
                {
                 }

请帮忙。

5 个答案:

答案 0 :(得分:2)

您永远不会在College课程中实例化学生列表。大学和学生课程中的其他对象也是如此。

public partial class College   
{ 
  public CollegeDetails collegeDetails;
  //Never instantiate
  //public List<Students> students;      
  //Should be:
  public List<Students> students = new List<Students>();

  public StaffDetails staffDetails;   
} 

答案 1 :(得分:2)

您需要向构造函数添加StudentsMarks的创建。

public College()
{
   Students = new List<Students>();
}

public Students()
{
  Marks = new List<Marks>();
}

或者如果构造函数不可用,则在使用前创建这些列表

答案 2 :(得分:2)

仅仅因为你已经宣布了学院和学生的实例并不意味着你已经创建了包含对象的新实例。您还需要创建它们的实例:

public partial class College {
  public College() {
    CollegeDetails = new CollegeDetails();
    Students = new List<Students>();
    StaffDetails = new StaffDetails();
  }
  public CollegeDetails CollegeDetails;
  public List<Students> Students;
  public StaffDetails StaffDetails;
}

public partial class Students {
  public Students() {
    StudentDetails = new StudentDetails();
    Marks = new List<Marks>();
  }
  public StudentDetails StudentDetails;
  public List<Marks> Marks;
}

BTW:不是变量名称的绝佳选择。

答案 3 :(得分:1)

您需要执行与以下类似的操作,以便复合类型公开的类型不是null

public partial class College
{
    public College()
    {
        CollegeDetails = new CollegeDetails();
        Students = new List<Student>();
        StaffDetails = new StaffDetails();
    }
    public CollegeDetails CollegeDetails;
    public List<Students> Students;
    public StaffDetails StaffDetails;
}

public partial class Student
{
    public Student()
    {
        StudentDetails = new StudentDetails();
        Marks = new List<Mark>();
    }
    public StudentDetails StudentDetails ;
    public List<Marks> Marks;
}

关于上述内容的几点说明:

在创建实例时,只调用一次类型的构造函数,这样就可以做好准备以供使用,可以这么说;在这种情况下,需要实例化您希望由外部源使用的类型(任何public)。

我将Students重命名为Student(非复数),因为List<Student>可以通过名为Students的属性访问来实现多个;我怀疑你可以为Mark/s做同样的事情。

答案 4 :(得分:0)

在我看来,像College对象的构造函数没有实例化学生列表。