对象引用未设置为存储库中对象的实例

时间:2019-06-25 07:21:43

标签: c# asp.net asp.net-mvc asp.net-core

我找不到问题。这是错误本身-NullReferenceException:对象引用未设置为对象的实例。 IChallengeRepository.cs中的RD_71.Repository.ChallengeRepository.CantUserMarkDay(字符串userEmail,DateTime时间),行47

这是Chrome写的电话链 1.ViewBag.IsStart = callRepository.IsStart(userEmail); 2.public bool IsStart(字符串userEmail) 3.返回dbDapper.Query(查询,新的{userEmail})。 ToList(); 4.TimeSpan span =(时间-新的DateTime(1970,1,1,0,0,0,0).ToLocalTime());

单击索引控制器时发生错误。这是我所有的存储库和控制器

[HttpGet]
        public IActionResult Index(string userEmail)
        {
            ViewBag.IsStart = challengeRepository.IsStart(userEmail);

            ProfileViewModel model = challengeRepository.GetProfileViewModel(userEmail);
            if (ViewBag.IsStart)
                model.UsetTask = JsonConvert.DeserializeObject<List<string>>(model.LastChallenge.UserTask);


            if (model.LastChallenge == null)
            {
                Challenge challenge = new Challenge { DaysComplete = 0 };
                model.LastChallenge = challenge;
            }
            return View(model);
        }

IsStart方法

public bool IsStart(string userEmail)
        {
            List<Challenge> list = GetUserChallenge(userEmail);

            if (list.Count == 0)
            {
                return false;
            }
            else
            {
                if (list[list.Count - 1].DaysComplete != 71)
                {
                    return true;
                }
            }
            return false;
        }

GetUserChallenge

public List<Challenge> GetUserChallenge(string userEmail)
        {
            using(IDbConnection dbDapper = new SqlConnection(connectionString))
            {
                string query = "SELECT * FROM Challenge WHERE UserEmail = @userEmail";
                return dbDapper.Query<Challenge>(query, new { userEmail }).ToList();
            }
        }

CanUserMarkDay

public bool CanUserMarkDay(string userEmail, DateTime time)
        {
            Challenge challenge = GetLastChallenge(userEmail);
            if(challenge != null)
            {
                TimeSpan span = (time - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
                TimeSpan span2 = (Convert.ToDateTime(challenge.LastDayComleteTime) - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());

                int res = span.Days - span2.Days;

                if (res > 0)
                    return true;
                else
                    return false;
            }
            return true;


        }

我不知道问题出在哪里,因为我的CanUserMarkDay方法仅在此控制器中被调用,但并未在任何地方使用

[HttpPost]
        public int CanUserMark(string userEmail)
        {
            if (challengeRepository.CanUserMarkDay(userEmail, DateTime.Now))
                return 0;
            else
                return 1;
        }

0 个答案:

没有答案