Problem with null variable

4

I'm developing C # software with the MVVM template , and with Visual Studio.

It is software to manage the members of a university.

We also have to use a database with Code First.

My problem is that it gives me the following error:

  

"An exception of type 'System.NullReferenceException' occurred in   UniversityMembers.exe but was not handled in user code   Additional information: Object reference not set to an instance of an object. "

I'll point the error in the code below:

public partial class MainWindow : Window
{
    private static VMUniversityMembers d = new VMUniversityMembers();
    ProjectContext db = d.ProjectContext;
    public MainWindow()
    {
        InitializeComponent();

        using (db)
        {

            var query = (DataContext as VMUniversityMembers).ProjectContext.UniversityMembers.SqlQuery("Select * from UniversityMembers");  **<---- Dá o erro aqui**

            foreach (var item in query)
            {
                listView.Items.Add(item);
            }
        }
    }
}

"VM UniversityMembers" is the View Model between the "UniversityMembers" Template and the graphical interface:

namespace UniversityMembers.ViewModel
{
    class VMUniversityMembers:BaseModel
    {
        private ProjectContext projectContext;
        private UniversityMember selectedMember = null;

        public List<UniversityMember> Members
        {
            get
            {
                return projectContext.UniversityMembers.Include("Position").Include("Sex").ToList();
            }
        }

        public ProjectContext ProjectContext
        {
            get
            {
                return projectContext;
            }
            set
            {
                projectContext = value;
            }

        }
        public UniversityMember SelectedMember
        {
            get
            {
                return selectedMember;
            }

            set
            {
                selectedMember = value;
            }
        }

        /**
         * Método para adicionar alunos 
         */

        internal void Add()
        {
            var unmbm = new UniversityMember();    

            projectContext.UniversityMembers.Add(unmbm);
            this.selectedMember = unmbm;
            this.OnPropertyChanged("UniversityMembers");    

        }
        internal void Cancel()
        {

        }
        /**
         *Método  para guardar a informacao introduzida  
         *
         */
        internal void SaveContext()
        {
            projectContext.SaveChanges();
            this.OnPropertyChanged("UniversityMembers");

        }
        /**
        * Método para eliminar um utilizador seleccionado na lista 
        * 
        */
        internal void Delete()
        {
            projectContext.UniversityMembers.Remove(SelectedMember);
        }    
    }
}

And the template I use is the UniversitYMembers:

    namespace UniversityMembers.Models
{
    class UniversityMember : Person
    {           
       public string email;
      // public  Position position;
       public string migration;


        public string Email { get { return email; } set { email = value; } }
      //  public Position Position { get { return position; } set { position = value; } }

        public UniversityMember()
        {

        }
    }
}

The class UniversityMembers extends in the superclass Person , with other attributes.

    
asked by anonymous 09.11.2016 / 16:37

1 answer

5

I will consider that DataContext is of type VMUniversityMembers , if it is not, there is the error. It seems to be later.

The ProjectContext property does not seem to be initializing anywhere, it looks like the problem is there.

UniversityMembers is certainly not initialized, there is also an error there.

Some other comments:

using (db)

This does not work, that's not how you use it, it will not release anything there, or worse, it will release what you can not, but it's another matter.

This:

public string email;
public string Email { get { return email; } set { email = value; } }

is the same as this:

public string Email { get; set; }

The same goes for all other properties that do something so simple and standardized.

    
09.11.2016 / 16:51