Object reference not set to an instance of an object error though I am instantiationg the object [closed]
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;
}
in my aspx.cs file
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
{
开发者_如何学JAVA college.students.Add(new students{ });
}
Addtogrid();
}
}
else
{
if (Session["xml"] != null)
{
}
Please help.
You need to add creation of Students
and Marks
to constructors.
public College()
{
Students = new List<Students>();
}
public Students()
{
Marks = new List<Marks>();
}
Or if constructors are unavailable then create those Lists before usage
You never instantiate the Student List inside the College class. Same goes for other objects in the College and Student classes.
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;
}
Just because you've declared an instance of College and Student does not mean you've created new instances of the containing objects. You need to create instances of them, too:
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: Not a great choice of variable names.
You're going to need to do something similar to the following so that your types which are exposed by your composite types are not 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;
}
A few notes on the above:
The constructor of a type is called just once, when the instance is created, which allows you to make ready for use, so to speak; in this case that entails instantiating your types that are expected to be used by an outside source (anything public
).
I renamed Students
to Student
(non-plural), as a List<Student>
achieves plurality by being accessible through a property named Students
; I'd suspect you could do the same for Mark/s
.
Looks to me like the constructor of the College object does not instantiate the Students list.
精彩评论