0

Here is my problem: I am trying to create an instance of a Student class which is the return type of the static method too, but for some reason I am getting the error

Object reference not set to an instance of an object

Here is my class

public class Student
{        
        static string VirtualFPath = "~/App_Data/Students.xml";

        public int ID { get; set; }
        public string Name { get; set; }
        public int CourseID { get; set; }

        public static string Serialize(Student student)
        {
            XElement xElement = new XElement("student", new XAttribute("id", student.ID));
            xElement.Add(new XElement("name", student.Name));
            xElement.Add(new XElement("course", student.Name));
            return xElement.ToString();
        }

        public static Student Deserialize(string studentxElement)
        {
            XElement xElement = XElement.Parse(studentxElement);

            // error occurs here
            Student stud = new Student
            {
                ID = Convert.ToInt32(xElement.Attribute("id").Value),
                Name = xElement.Element("name").Value,
                CourseID = Convert.ToInt32(xElement.Element("course").Value)
            };

            return stud;
        }

        public static List<Student> GetAllStudents()
        {
            List<Student> StudentsList = new List<Student>();

            XDocument xDoc = XDocument.Load(HttpContext.Current.Server.MapPath(VirtualFPath));            

            var xStudentElement = from xStudent in
                                    xDoc.Descendants("students")
                                    select xStudent;

            foreach (XElement xStudent in xStudentElement)
            {
                Student student = Student.Deserialize(xStudent.ToString());
                StudentsList.Add(student);
            }

            return StudentsList;
        }
}

I call GetAllStudents() by creating an instance of the Student class.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
kartik rajan
  • 115
  • 1
  • 5
  • 2
    It would be better if you point us to the exact line giving the exception. However this is a duplicate of a well known canonical question – Steve Apr 20 '18 at 07:06
  • 1: During deserialization you don't check if named attributes or elements are present before accessing them. 2: Why don't you use the default XmlSerializer for serialization / deserialization? – Alexander Powolozki Apr 20 '18 at 07:09
  • Debug your code. Either `xElement.Attribute("id")`, `xElement.Element("name")` or `xElement.Element("course")` returns `null`, causing `.Value` to throw. – CodeCaster Apr 20 '18 at 07:10
  • `XDocument xDoc = XDocument.Load(HttpContext.Current.Server.MapPath(VirtualFPath)); ` does this line provide proper result ? or `xDoc` stay null after execution ? – Amit Apr 20 '18 at 07:10
  • Thank you for your replies. changing var xStudentElement = from xStudent in xDoc.Element("students").Elements("student") select xStudent; seemed to work for me. – kartik rajan Apr 20 '18 at 07:35

0 Answers0