0

I'm using MVC 4 with EF 4.5. I have a class Evaluation and a class Component:

public class Component
    {
        public virtual int ComponentId { get; set; }

        public virtual string Type { get; set; }
        public virtual string Name { get; set; }

        public virtual Course Course { get; set; }

        public virtual int ParentId { get; set; }
        public virtual Component Parent { get; set; }
        public virtual List<Component> Childs { get; set; }

        public virtual List<Evaluation> Evaluations { get; set; }

}

public class Evaluation
    {
        public virtual int EvaluationId { get; set; }

        public virtual int ComponentId { get; set; }
        public virtual Component Component { get; set; }

        public virtual int UserId { get; set; }
        public virtual User User { get; set; }

        public virtual int Grade { get; set; }
    }

As you can see the component have a parent-child relationship. I need to give a grade to a component to an user. The grade is only given to the last child of the component and the parents will get the same grade until the parent is 0. Example:

User1:
    Component1 -> The grade would be the sum of Child1 and Child2
      Child1 -> The grade would be the sum of Subchild1 and Subchild2 grades
        Subchild1 -> Insert grade
        Subchild2 -> Insert grade
      Child2 -> Insert grade

I have this method:

private void method(int parent, int grade)
        {

            if (parent != 0)
            {
                var c = db.Components.Where(h => h.ComponentId == parent).FirstOrDefault();
                var a = db.Evaluations.Where(b => b.ComponentId == c.ComponentId).FirstOrDefault();


                if(a == null) <- ERROR
                {
                    var avaModl = new Evaluation <- ERROR
                    {                     
                        ComponentId = c.ComponentId,
                        parent = c.ParentId,
                        Grade = grade,
                        UserId = a.UserId  
                    };
                    db.Evaluations.Add(avaModl);
                    db.SaveChanges();

                    if(c.ParentId != 0)
                    {
                    method(c.ParentId, avaModl.Grade);
                    } else
                    {
                    method(c.ComponentId, avaModl.Grade);
                    }
                } else
                {
                    a.Grade += grade;
                    if (c.ParentId != 0)
                    {
                       method(c.ParentId, a.Grade);
                    }
                    else
                    {
                       method(c.ComponentId, a.Grade);
                    }
                }

            } else
            {
                var c = db.Components.Where(h => h.ComponentId == parent).FirstOrDefault();
                var a = db.Evaluations.Where(b => b.ComponentId == c.ComponentId).FirstOrDefault();

                if (a == null)
                {
                    var avaModl = new Evaluation
                    {                 
                        ComponentId = c.ComponentId,
                        parent = c.ParentId,
                        Grade = grade,
                        UserId = a.UserId 
                    };
                    db.Evaluation.Add(avaModl);
                    db.SaveChanges();
                }
                else
                {
                    a.Grade += grade;
                }
            }
        }

That i call when i introduce a grade and an user to a component. The problem is that it gives the NullPointerException error and i don't understand why. Can someone explain me why its giving that error and how to resolve it? I already saw some questions and faqs about this error but i can't resolve it.. Thanks

dasdasd
  • 97
  • 1
  • 14
  • Post the exact error message you get. Also, a tip, if you're writing `ERROR` in your code, make it a comment, so it is easier to see and doesn't trip up people trying to compile your code – smead Aug 22 '16 at 23:23
  • 1
    The problem is fairly obvious: `if (a == null) { ... UserId = a.UserId` – Blorgbeard Aug 22 '16 at 23:26

0 Answers0