Person, Teacher , Student CRUD

Algorithme T

Member
Joined
Sep 26, 2022
Messages
15
Programming Experience
Beginner
Hello,
I am trying to get a Person, Teacher,Student Crud done .
I initially worked with Person, Teacher,Student abstract classes (every thing was working perfectly well) then i decided to use those abstract classes as models and created 3 other classes which are Services such as PersonService , TeacherService ....

I am getting an error of "Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object." each time i am trying to use a method which are now found in the Services classes


<
My AddPerson Function:
PersonService? p = null;

            Person? pChange = null;

            switch (type)
            {
                case "student":
                    
                    if (lastName != null && id != null)
                    {
                        pChange = new Student(lastName, firstName, dateOfBirth, id);
                        try
                        {
                            p.Add(pChange);
                        }
                        catch(Exception ex)
                        {
                            Console.WriteLine("Success ");
                        }
                    }
                    break;
                case "teacher":
                    
                    int salary;
                    try
                    {
                         salary = Convert.ToInt32(Console.ReadLine());
                    }
                    catch (Exception ex)
                    {
                        
                        salary = 0;

                    }

                    if (lastName != null)
                    {
                        pChange = new Teacher(lastName, firstName, dateOfBirth, hireDate, salary);
                        p.Add(pChange);
                        
                    }
/>
 
On which line are you getting the exception?
 
The error seems accurate. p was set to null on line 1. Perhaps p should be set to reference an instance of the PersonService.
 
See my post #4.
 
Scroll up.
 
When you call new, you create a new instance of an object. Therefore:
C#:
p = new PersonService();
[/icode]
 
Then instantiate a concrete class seems to be the logical solution.
 

Latest posts

Back
Top Bottom