如何修复"对象引用未设置为对象的实例。" C#

时间:2014-09-11 04:18:21

标签: c# asp.net .net

public long CheckUser(int country_code, long mobile_no)
{
  LociDataClassesDataContext dc = new LociDataClassesDataContext();
  var ret = dc.registrations.FirstOrDefault(a=>a.country_code.Equals(country_code) && a.mobile_number.Equals(mobile_no));
  if (ret != null)
  {
    return ret.id;
  }
  else
  {
    return 0;
  }
}

/// <summary>
/// 
/// </summary>
/// <param name="os_id"></param>
/// <param name="device_id"></param>
/// <param name="country_code"></param>
/// <param name="mobile_no"></param>
/// <returns></returns>
public DefaultRespons GetRegister(int os_id, string device_id, int country_code, long mobile_no)
{
  LociDataClassesDataContext dc = new LociDataClassesDataContext();
  int  reg_id = (int) CheckUser(country_code, mobile_no);
  if (reg_id == 0)
  {
    registration reg = new registration();
    reg.os_id = os_id;
    reg.device_id = device_id;
    reg.country_code = country_code;
    reg.mobile_number = mobile_no;
    reg.verification_code = new Random().Next(1000, 9999);
    dc.registrations.InsertOnSubmit(reg);
    dc.SubmitChanges();
    Twilio.TwilioRestClient client = new Twilio.TwilioRestClient("AC3c23fee017f23f5061a6b5d3be6f74da", "6fe81560f88f3850c5ad5d4a7b8a5f50");
    Twilio.SMSMessage message = client.SendSmsMessage("+16782493911", "+" + reg.country_code + "" + reg.mobile_number, "Your verification code for Locii is: " + reg.verification_code);
    if (message.RestException != null)
      Debug.WriteLine(message.RestException.Message);
    return new DefaultRespons(1, "OK", Registration.getResponse(reg));
  }
  else
  {
    return new DefaultRespons(1, "OK",reg_id);
  }
}

这是我的代码我在调试代码时遇到异常

if (message.RestException != null)
  Debug.WriteLine(message.RestException.Message);

以下是错误代码:

  

{“消息”:“发生了错误。”,“ExceptionMessage”:“对象   引用未设置为的实例   对象 “” ExceptionType。 “:” System.NullReferenceException “ ”堆栈跟踪“:”   在   LociWebServices.Controllers.RegistrationController.GetRegister(的Int32   os_id,String device_id,Int32 country_code,Int64 mobile_no)in   d:\更新\ LociWebServices \ LociWebServices \ LociWebServices \ \控制器RegistrationController.cs:行   75 \ r \ n在lambda_method(Closure,Object,Object [])\ r \ n at   System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor&LT;&GT; c__DisplayClass13.b__c(对象   instance,Object [] methodParameters)\ r \ n at   System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(对象   instance,Object [] arguments)\ r \ n at   System.Web.Http.Controllers.ReflectedHttpActionDescriptor&LT;&GT; c__DisplayClass5.b__4(个)\ r \ n   在System.Threading.Tasks.TaskHelpers.RunSynchronously [TResult](Func`1   func,CancellationToken cancellationToken)“}

请帮我解决这个问题。

0 个答案:

没有答案