当前位置: 代码迷 >> .NET相关 >> 出错 object reference not set to an instance of an object,是什么原因
  详细解决方案

出错 object reference not set to an instance of an object,是什么原因

热度:183   发布时间:2016-04-24 03:05:14.0
报错 object reference not set to an instance of an object,是什么原因啊
class Customer
    {
......
        public bool save()
        {
            if (this.Id == -1)
            {
                int customerId = DataManager.addCustomer(this);//此处报错
                if (customerId == -1) return false;
                else
                {
                    this.Id = customerId;
                    return true;
                }
            }
            else
            {
                DataManager.UpdataCustomer(this);
                return true;
            }
        }
    }

static class DataManager
{
static public int addCustomer(Customer customer)
        {
            if (getCustomerId(customer.Name)!=-1) return -1;
            tb_Customer tb_customer = new tb_Customer();
            tb_customer.Id = customer.Id;
            tb_customer.Name = customer.Name;
            tb_customer.Password = customer.Password;
            tb_customer.PhoneNumber = customer.PhoneNumber;
            dataContext.tb_Customer.InsertOnSubmit(tb_customer);
            return getCustomerId(customer.Name);
        }
}
------解决方案--------------------
对象未实例化,不能是this,new一个
  相关解决方案