NullReferenceException al agregar un registro a la tabla

I am receiving a NullReferenceException when trying to add a record to a table using LINQ on WP7. I am relatively new to C#/LINQ so I have copied one of my existing methods which works OK, but now I cannot make it work for the new record. The code is below;

private ObservableCollection<DBControl.Categories> _category;
    public ObservableCollection<DBControl.Categories> Category
    {
        get
        {
            return _category;
        }
        set
        {
            if (_category != value)
            {
                _category = value;
                NotifyPropertyChanged("Category");
            }
        }
    }


    private void button1_Click(object sender, RoutedEventArgs e)
    {

        string TestCategory = "Cars";

        // Create a new to-do item based on the text box.
        DBControl.Categories newCat = new DBControl.Categories { CategoryDesc = TestCategory };
        //CategoryDesc

        // Add a to-do item to the observable collection.
        **Category.Add(newCat);**

        // Add a to-do item to the local database.
        BoughtItemDB.Category.InsertOnSubmit(newCat);

        BoughtItemDB.SubmitChanges();
    }

The line of code which is giving me the error is Category.Add(newCat)

As far as I can tell everything looks OK which probably means I've made a daft mistake (again).

Cualquier ayuda es muy apreciada.

The table definition is below;

[Table(Name = "Categories")]
    public class Categories : INotifyPropertyChanged, INotifyPropertyChanging
    {
        // Define ID: private field, public property and database column.
        private int _categoryId;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int CategoryId
        {
            get
            {
                return _categoryId;
            }
            set
            {
                if (_categoryId != value)
                {
                    NotifyPropertyChanging("CategoryId");
                    _categoryId = value;
                    NotifyPropertyChanged("CategoryId");
                }
            }
        }

        // Define item category: private field, public property and database column.
        private string _categoryDesc;

        [Column]
        public string CategoryDesc
        {
            get
            {
                return _categoryDesc;
            }
            set
            {
                if (_categoryDesc != value)
                {
                    NotifyPropertyChanging("CategoryDesc");
                    _categoryDesc = value;
                    NotifyPropertyChanged("CategoryDesc");
                }
            }
        }
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify the page that a data context property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify the data context that a data context property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }

preguntado el 01 de febrero de 12 a las 22:02

1 Respuestas

You need to initialize Category or _category. It's not getting set to any value, so you're trying to Add() to a non-initialized object.

Respondido 02 Feb 12, 02:02

Thanks for this - exactly what I needed to do. The other properties were initialised by a call that I hadn't seen. This works differently to VB which is what I am migrating from. - MAO

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.