0

所以!我的头刚刚爆炸,因为这对我来说似乎很奇怪。使用调试模式,我可以看到 Claimsresponse 进入方法,它被填充,Email 属性也是如此。但是当我尝试一个简单的字符串 myStr = response.Email; 它抛出一个 NullException 错误......

我不明白。。

代码:(设置电子邮件失败!)

public static bool LoginUserUsingOpenID(BaseDBContext db, ClaimsResponse r)
            {
                string email = r.Email;
                bool doesntExist = !UserHelper.DoesUserExist(db, r.Email);

                User u = null;

                if (doesntExist)
                {
                    u.Email = email;
                    u.Username = "User" + new Random().Next(100000);
                    u.Password = CreateRandomPassword(7);
                    u.prepareForCreationFromThirdParty();
                }
                else
                {
                    u = db.Users.Where(x => x.Email == r.Email).FirstOrDefault();
                }
                SetUserAsAuthenticated(u);
                AddAutomaticLoginKeyForUser(db, u); //Need to get this to the users frontend somehow..
                return true;
            }

这是堆栈跟踪:

System.NullReferenceException: Object reference not set to an instance of an object.
   at MusingMonkey.Helpers.SecurityHelper.LoginUserUsingOpenID(BaseDBContext db, ClaimsResponse r) in C:\Users\William-Business\Desktop\TWB\TWB Central\Projects\MusingMonkey\MusingMonkey\Helpers\SecurityHelper.cs:line 170
   at MusingMonkey.Controllers.UsersController.HandleOpenIDResponse() in C:\Users\William-Business\Desktop\TWB\TWB Central\Projects\MusingMonkey\MusingMonkey\Controllers\UsersController.cs:line 78
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)
4

1 回答 1

1

呃,你确定那r.Email是抛出 NRE 的那条线吗?

因为烟雾似乎来自以下线路:

User u = null;

然后你试图在u没有实例化这个变量的情况下访问实例:

if (doesntExist)
{
    u.Email = email;
    ...

所以你可能意味着别的东西,例如:

User u = new User();
于 2012-08-19T08:47:54.643 回答