9 Replies - 1035 Views - Last Post: 30 November 2012 - 10:00 AM Rate Topic: -----

#1 mel_ga   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 32
  • Joined: 18-January 10

Getting an exception error

Posted 26 November 2012 - 09:02 AM

Hi I have a list of students and a list of marks. When I try to get the Id of a particular student to use it to add his marks in the AddMarks method I am getting a null exception error. Can someone help me fix it please. Here is the code:
   public class Student
    {
        private string name;
        private string surname;
        private string dob;
        private string address;
        private string id;
     

        public Student()
        {
            //empty contructor
        }


        public Student(string name, string surname, string dob, string address, string id)
        {
            this.name = name;
            this.surname = surname;
            this.dob = dob;
            this.address = address;
            this.id = id;
        }

      

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Surname
        {
            get { return surname; }
            set { surname = value; }

        }

        public string DOB
        {
            get { return dob; }
            set { dob = value; }
        }

        public string Addr
        {
            get { return address; }
            set { address = value; }

        }

        public string Id
        {
            get { return id; }
            set { id = value; }
        }

    }
}

public struct Marks
{
    private int hyexam;
    private int anexam;

    public int HYEXAM
    {
        get { return hyexam; }
        set { hyexam = value; }
    }

    public int ANEXAM
    {
        get { return anexam; }
        set { anexam = value; }
    } 

    public Marks (int hyexam, int anexam)
    {
        this.hyexam = hyexam;
        this.anexam = anexam;
    }

    public double OverallExam()
    {
        return (0.4 * hyexam) + (0.6 * anexam);
    }

    
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace Main
{
    public class JuniorStudents : Student
    {
        public int numofStudents;
        public Marks Mathematics;
        public Marks English;
        public Marks Maltese;
        public Marks ReligiousStudies;
        public Marks SocialStudies;
    
        const string filestud = "C:\\Users\\Mel\\Desktop\\Students.txt";
        const string filemarks= "C:\\Users\\Mel\\Desktop\\Marks.txt";
        public List<Student> studentList = new List<Student>();
        public Dictionary<Student, List<Marks>> stuID = new Dictionary<Student, List <Marks>>();
        List<Marks> markslist = new List<Marks>();
        public void ReadStudentsRecords()
        {
          
           try
            {
                string name;
                string surname;
                string dob;
                string addr;
                string id;
                string year;
                StreamReader myFile = File.OpenText(filestud);
                while (myFile.ReadLine() != null)
                {
                    name = myFile.ReadLine();
                    surname=myFile.ReadLine();
                    dob=myFile.ReadLine();
                    addr=myFile.ReadLine();
                    id=myFile.ReadLine();
                    year = myFile.ReadLine();
                    Student st = new Student(name, surname, dob, addr, id);
                    Year yr = new Year(year);
                    studentList.Add(st);

                }
                myFile.Close();
            }
            catch (Exception e)
            {
                e.ToString();
            }
               
            }

        public void ReadMarksList()
        {
            try {                     
                StreamReader myFile = File.OpenText(filemarks);
                while (myFile.ReadLine() != null)
                {
                       
                        Mathematics.HYEXAM = myFile.Read();
                        Mathematics.ANEXAM = myFile.Read();
                        English.HYEXAM = myFile.Read();
                        English.ANEXAM = myFile.Read();
                        Maltese.HYEXAM = myFile.Read();
                        Maltese.ANEXAM = myFile.Read();
                        ReligiousStudies.HYEXAM = myFile.Read();
                        ReligiousStudies.ANEXAM = myFile.Read();
                        SocialStudies.HYEXAM = myFile.Read();
                        SocialStudies.ANEXAM = myFile.Read();

                        markslist.Add(Mathematics);
                        markslist.Add(English);
                        markslist.Add(Maltese);
                        markslist.Add(ReligiousStudies);
                        markslist.Add(SocialStudies);
                }
                myFile.Close();
            }
            catch (Exception e)
            {
                e.ToString();
            }
        }
             
        
             
        public void WriteStudentRecords()
        {   
            try
            {
                StreamWriter myFile = File.CreateText(filestud);
                foreach (Student s in studentList)
                {
                    myFile.WriteLine(s.ToString());
                }
                myFile.Close();
            }
            catch (Exception e)
            {
                e.ToString();
            }
        }

        public void WriteMarksRecords()
        {
             try
            {
                StreamWriter myFile = File.CreateText(filemarks);
                foreach (Marks m in markslist)
                {
                    myFile.WriteLine(m.ToString());
                }
                myFile.Close();
            }
            catch (Exception e)
            {
                e.ToString();
            }
        }

        //public void AddrecordHashtable()
        //{
        //    Student stud =new Student();
        //    if (!stuHashtable.ContainsKey((string)Id))
        //    {
        //        stuHashtable.Add(Id, stud);
        //    }
        //     foreach(DictionaryEntry gg in stuHashtable)
        //    {
        //        Console.WriteLine("Key  and  value are " + gg.Key + "   "  + gg.Value);
        //        Console.Read();
        //    }
        //}


        public void AddStudentRecord()
        {

            Student stud = new Student(Name, Surname, DOB, Addr, Id);
                studentList.Add(stud);
             


                //bool error = false;
                //if (error == false)
                //{
                //    try
                //    {
                //        stud.Name = name;
                //        stud.Surname = surname;
                //        stud.DOB = dob;
                //        stud.Addr = address;
                //        stud.Id = id;

                //    }
                //    catch (FormatException ex)
                //    {
                //        error = true;
                //        Console.WriteLine(ex.ToString());
                //    }


                //myFile.WriteLine(studentList);
                // numofStudents = studentList.Count;
            }
        
          //  return 1;
        //public void StudentID(Student key)
        //{
        //    bool isFound = false;
        //    //key = studentList.SingleOrDefault(
        //    //if (studentList.Id)
        //    //{
        //    //    if (st.Id.Equals(key))
        //    //    {
        //    foreach (Student id in studentList) {
        //        if (id==key) {
        //            isFound = true;
        //            Console.WriteLine("key found");
        //        }
            
        //        else
        //        {
        //            Console.WriteLine("id not found");
        //            isFound = false;
        //        }
        //    }
        //}

        public void AddMarksRecord(int mthyexam, int mtanexam, int enhyexam, int enanexam, int mlhyexam, int mlanexam, int rshyexam, int rsanexam, int sshyexam, int ssanexam)
        {
            bool error = false;
            if (error == false)
            {

                try
                {
                    //  Student myLocatedObject = studentList.Find(delegate(Student s) { return s.Id == Id; });
                    //   foreach (Student s in studentList)
                    //  if (s.Id.Equals(index))
                    {
                        //  AddrecordHashtable();
                     
                            //Mathematics.SUBJECTID = "1";
                            //Maltese.SUBJECTID = "2";
                            //Maltese.SUBJECTID = "3";
                            //ReligiousStudies.SUBJECTID = "4";
                            //SocialStudies.SUBJECTID = "5";
                            
                            Mathematics.HYEXAM = mthyexam;
                            Mathematics.ANEXAM = mtanexam;
                            English.HYEXAM = enhyexam;
                            English.ANEXAM = enanexam;
                            Maltese.HYEXAM = mlhyexam;
                            Maltese.ANEXAM = mlanexam;
                            ReligiousStudies.HYEXAM = rshyexam;
                            ReligiousStudies.ANEXAM = rsanexam;
                            SocialStudies.HYEXAM = sshyexam;
                            SocialStudies.ANEXAM = ssanexam;
                          

                        }
                    }
                
                catch (FormatException ex)
                {
                    error = true;
                    Console.WriteLine(ex.ToString());
                }
            }
           
                //    Console.WriteLine ("id not found");
                //}

                //markslist.Add(Mathematics);
                //markslist.Add(English);
                //markslist.Add(Maltese);
                //markslist.Add(ReligiousStudies);
                //markslist.Add(SocialStudies);           
                //}
           

            }
        
        
       
    

        public int EditStudentRecord(int index)
        {
            Student stud = new Student(Name,Surname,DOB,Addr,Id);   
            //bool error=false;
            //if (error==false) {
            //   try {
            //stud.Name = name;
            //stud.Surname=surname;
            //stud.DOB=dob;
            //stud.Addr=addr;
            //stud.Id=id;
            //Mathematics.HYEXAM = mthyexam;
            //Mathematics.ANEXAM = mtanexam;
            //English.HYEXAM = enhyexam;
            //English.ANEXAM = enanexam;
            //Maltese.HYEXAM = mlhyexam;
            //Maltese.ANEXAM = mlanexam;
            //ReligiousStudies.HYEXAM = rshyexam;
            //ReligiousStudies.ANEXAM = rsanexam;
            //SocialStudies.HYEXAM = sshyexam;
            //SocialStudies.ANEXAM = ssanexam;


            //   }
            //    catch  (FormatException ex){
            //    error=true;
            //    Console.WriteLine (ex.ToString());
            //    }

            studentList[index] = stud;
            //studentList[index] = Mathematics;
            //studentList[index] = English;
            //studentList[index] = Maltese;
            //studentList[index] = ReligiousStudies;
            //studentList[index] = SocialStudies;
            //numofStudents = studentList.Count;
            //StreamWriter myFile = File.AppendText("JuniorStudents.txt");
            studentList.Add(stud);
            //myFile.WriteLine(studentList);
            
            return 1;
        }
 
        public int DeleteRecord(int index)
        {
            studentList.RemoveAt(index);
            WriteStudentRecords();
            return 1;
        }

         public void ViewRecords()
        {
            ReadStudentsRecords();
            ReadMarksList();
            Console.WriteLine("Welcome to Student Mark List Application");
            //for (int i = 0; i <numofStudents; i++)
            //{
                //Console.WriteLine(studentList[i]);
               Console.WriteLine(Name);
                Console.WriteLine(Surname);
                Console.WriteLine(Addr);
                Console.WriteLine(DOB);
                Console.WriteLine(Id);
                Console.WriteLine(Mathematics.HYEXAM);
                Console.WriteLine(Mathematics.ANEXAM);
                Console.WriteLine(English.HYEXAM);
                Console.WriteLine(English.ANEXAM);
                Console.WriteLine(Maltese.HYEXAM);
                Console.WriteLine(Maltese.ANEXAM);
                Console.WriteLine(ReligiousStudies.HYEXAM);
                Console.WriteLine(ReligiousStudies.ANEXAM);
                Console.WriteLine(SocialStudies.HYEXAM);
                Console.WriteLine(SocialStudies.ANEXAM);               
          //  }
        }
         public void InputStudentRecords()
        {
            while (true)
            {
                string name;
                string surname;
                string dob;
                string addr;
                string id;
                string year;
                Console.WriteLine("Welcome to Student Mark List Application");
                Console.WriteLine("Enter student Name: ");
                name = Console.ReadLine();
                Console.WriteLine("Enter student Surame: ");
                surname = Console.ReadLine();
                Console.WriteLine("Enter student Dob: ");
                 dob = Console.ReadLine();
                Console.WriteLine("Enter student Address: ");
                addr = Console.ReadLine();
                Console.WriteLine("Enter student ID: ");
                id = Console.ReadLine();
                Console.WriteLine("enter student year:");
                year = Console.ReadLine();
                AddStudentRecord();
                              
                Console.WriteLine("Do you want to add another record (Y/N)? ");
                char ch = Console.ReadKey().KeyChar;

                if (ch == 'Y' || ch == 'y')
                    continue;
                else
                    break;
            }
        }

         public void InputMarksRecords(string id)
         {
             while (true)
             {         
                 foreach (Student st in studentList)
                 {                  
                         if (st.Id.Equals(id))
                         {
                             Console.WriteLine("Enter Maths half yearly mark");
                             int m1 = Int32.Parse(Console.ReadLine());
                             Console.WriteLine("Enter Maths annual mark");
                             int m2 = Int32.Parse(Console.ReadLine());
                             Console.WriteLine("Enter English half yearly mark");
                             int e1 = Int32.Parse(Console.ReadLine());
                             Console.WriteLine("Enter English annual mark");
                             int e2 = Int32.Parse(Console.ReadLine());
                             Console.WriteLine("Enter Maltese half yearly mark");
                             int ml1 = Int32.Parse(Console.ReadLine());
                             Console.WriteLine("Enter Maltese annual mark");
                             int ml2 = Int32.Parse(Console.ReadLine());
                             Console.WriteLine("Enter ReligiousStudies half yearly mark");
                             int r1 = Int32.Parse(Console.ReadLine());
                             Console.WriteLine("Enter ReligousStudies annual mark");
                             int r2 = Int32.Parse(Console.ReadLine());
                             Console.WriteLine("Enter Social studies half yearly mark");
                             int s1 = Int32.Parse(Console.ReadLine());
                             Console.WriteLine("Enter SocialStudies annual mark");
                             int s2 = Int32.Parse(Console.ReadLine());
                             AddMarksRecord(m1, m2, e1, e2, ml1, ml2, r1, r2, s1, s2);
                         }
                         else
                         {
                             Console.WriteLine("enter correct id next time");
                         }
                         Console.WriteLine("Do you want to add another record (Y/N)? ");
                         char ch = Console.ReadKey().KeyChar;

                         if (ch == 'Y' || ch == 'y')
                             continue;
                         else
                             break;
                     }
                 }
             }
         

        public void EditRecords()
       {
           ViewRecords();
           Console.WriteLine("Enter the student you want to edit: ");
           int m = Convert.ToInt16(Console.ReadLine()); ;

           if (m >= 1)
           {  
               Console.WriteLine("Welcome to Student Mark List Application");
               Console.WriteLine("Enter student Name: ");
               string name = Console.ReadLine();
               Console.WriteLine("Enter student Surame: ");
               string surname = Console.ReadLine();
               Console.WriteLine("Enter student Dob: ");
               string dob = Console.ReadLine();
               Console.WriteLine("Enter student Address: ");
               string addr = Console.ReadLine();
               Console.WriteLine("Enter student ID: ");
               string id = Console.ReadLine();
               Console.WriteLine("Enter Maths half yearly mark");
               int m1 = Int32.Parse(Console.ReadLine());
               Console.WriteLine("Enter Maths annual mark");
               int m2 = Int32.Parse(Console.ReadLine());
               Console.WriteLine("Enter English half yearly mark");
               int e1 = Int32.Parse(Console.ReadLine());
               Console.WriteLine("Enter English annual mark");
               int e2 = Int32.Parse(Console.ReadLine());
               Console.WriteLine("Enter Maltese half yearly mark");
               int ml1 = Int32.Parse(Console.ReadLine());
               Console.WriteLine("Enter Maltese annual mark");
               int ml2 = Int32.Parse(Console.ReadLine());
               Console.WriteLine("Enter ReligiousStudies half yearly mark");
               int r1 = Int32.Parse(Console.ReadLine());
               Console.WriteLine("Enter ReligousStudies annual mark");
               int r2 = Int32.Parse(Console.ReadLine());
               Console.WriteLine("Enter Social studies half yearly mark");
               int s1 = Int32.Parse(Console.ReadLine());
               Console.WriteLine("Enter SocialStudies annual mark");
               int s2 = Int32.Parse(Console.ReadLine());
               EditStudentRecord(m - 1);
               Console.WriteLine("Record updated. Press any key to return to Main Menu");
               Console.ReadKey();
           }
           else
           {       
               Console.WriteLine("Invalid Entry. Press any key to return to Main Menu");
               Console.ReadKey();
           }

       }
         public void DeleteRecords()
        {
            ViewRecords();
            Console.WriteLine("Enter the student you want to delete: ");
            int m = Convert.ToInt16(Console.ReadLine()); ;

            if (m >= 1 && m <= numofStudents)
            {
                DeleteRecord(m - 1);
                Console.WriteLine("Record deleted. Press any key to return to Main Menu");
                Console.ReadKey(); ;
            }
            else
            {
                Console.WriteLine("Invalid Entry. Press any key to return to Main Menu");
                Console.ReadKey(); ;
            }
        }



        
    }
  }


This post has been edited by mel_ga: 26 November 2012 - 09:03 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Getting an exception error

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Getting an exception error

Posted 26 November 2012 - 09:11 AM

Null exception is the first error covered in the tutorial linked in my signature block "What does this error mean?"
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Getting an exception error

Posted 26 November 2012 - 09:17 AM

Also you didn't give us your complete program. Right now with the code you've posted, I'm thinking to myself "of course you'll get a null exception error in AddMarks() because you are accessing variables which have not been initialized as far as I can see."
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Getting an exception error

Posted 26 November 2012 - 09:29 AM

        const string filestud = "C:\\Users\\Mel\\Desktop\\Students.txt";


This is a very bad habit/thing to do. How would your program every work on any other user's computer?
There is a SpecialFolders enum you should use for this.




113        public List<Student> studentList = new List<Student>();
114        public Dictionary<Student, List<Marks>> stuID = new Dictionary<Student, List <Marks>>();
115        List<Marks> markslist = new List<Marks>();

A List of students, a dictionary of students & marks and a LIst of marks, plus an entire class for student...

I think you're missing the point of classes. If your marks class had both a class name and grade value in it, you could then just add a List<Marks> within the Student class. Let each student object take care of itself and don't make the program micro-manage everything. THere is a classes and objects tutorial linked in my signature block that should help.



Why is Marks a struct? Just make it a class like everything else.
If you give it a class name then you don't have to do this:
105        public Marks Mathematics;
106        public Marks English;
107        public Marks Maltese;
108        public Marks ReligiousStudies;
109        public Marks SocialStudies;

This is impossible to loop through. But you could loop through 1,000 grades if they are part of a List<Marks> where the Marks class includes the name and the grade.


Line 502 - You begin a method to edit a student record.
You get a bunch of values into local variables: Name, age etc.
Then you don't do anything with those values you collected.
Line 541 you call another edit method that we don't see here - But that won't know any of the local values you just entered using this method.



hyexam - Is that supposed to be HalfYearExam?
You aren't paying for these names by the byte. Use names that are meaningful both now, and 6 months from now when you haven't looked at this project for ages. HalfYearExamScore is a totally legal name that will make reading this code a lot less work.

I think you have a lot of great effort here. I really do. We all learn through experience. This is a great version 1 effort. I think if you read that article on classes, then start a version 2 of this project from scratch trying hard to plan such that each class is more complete you'll have a better solution. You want 'fuller' classes and 'emptier' program to control them. Let the class do most of the heavy lifting.
Was This Post Helpful? 1
  • +
  • -

#5 mel_ga   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 32
  • Joined: 18-January 10

Re: Getting an exception error

Posted 26 November 2012 - 01:40 PM

Could you explain to me please what classes should I have and their properties? Also there should be the inclusion of collections
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Getting an exception error

Posted 26 November 2012 - 02:58 PM

We typically don't design your program for you. It's up to you to come up with a design and we can try to steer you towards a better design.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Getting an exception error

Posted 26 November 2012 - 03:18 PM

I thought I just did.

No, I am not going to do all your software engineering/architecture/design. I have no need to layout all your homework so you can just be the code monkey and type up what someone else designed. That doesn't help you the least little bit.

This is your software, your homework, your grade. You learn through doing. As I said what you have now is a great version 1 effort. I was just trying to give you some comprehensive feedback because you seemed to be one of the few rookies we see that actually make an effort and I thought you might take that feedback and run with it.
Was This Post Helpful? 2
  • +
  • -

#8 mel_ga   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 32
  • Joined: 18-January 10

Re: Getting an exception error

Posted 30 November 2012 - 09:39 AM

Sorry to bother you again. I am trying to build the program again. When you say that the Marks class should include the name and grade what do you mean exactly. Can you give me an example please?
Was This Post Helpful? 0
  • +
  • -

#9 ipushmycar   User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: Getting an exception error

Posted 30 November 2012 - 09:44 AM

http://msdn.microsof...2(v=vs.80).aspx

The marks class should use the name and grade of a student as the fields.

In the example I linked,

the Person object uses just name as a field.
Was This Post Helpful? 0
  • +
  • -

#10 mel_ga   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 32
  • Joined: 18-January 10

Re: Getting an exception error

Posted 30 November 2012 - 10:00 AM

So in the Student class I don't declare a name because I'm not understanding what I have to declare
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1