简体   繁体   中英

nullable object must have value

I've checked the million other posts for the same error on here but couldn't find anything that helps.

I have a model like this:

public class User
{
    public int UserId { get; set; }
    public string UserName { get; set; }
    public int? CommunityId { get; set; }
    public string Email { get; set; }
    public Int64? Phone { get; set; }
    public AlertMode AlertMode { get; set; }
    public string B64EncodedImage { get; set; }
}

When I get the user from the database, the assignment looks like this:

User user = new User()
{
    UserId = Convert.ToInt32(reader["UserId"]),
    UserName = Convert.ToString(reader["UserName"]),
    CommunityId = reader["CommunityId"] == DBNull.Value
        ? (int?)null : Convert.ToInt32(reader["CommunityId"]),
    Phone = reader["Phone"] == DBNull.Value
        ? (Int64?)null : Convert.ToInt64(reader["Phone"]),
    AlertMode = (AlertMode)Int32.Parse(reader["AlertMode"].ToString()),
    Email = Convert.ToString(reader["Email"]),
    B64EncodedImage = Convert.ToString(reader["B64EncodedImage"])
};

When I call this code:

        @((BusinessLogic.GetUserByUserId(WebSecurity.CurrentUserId)).CommunityId.HasValue
                ? "COMMUNITYID"
                : "Not set!"
             )

I get this error:

Nullable object must have a value.

On this line:

@((BusinessLogic.GetUserByUserId(WebSecurity.CurrentUserId)).CommunityId.HasValue

Any ideas why?

====================================================================== EDIT:

I changed the code to return the User to the view as a model:

    public ActionResult Manage()
    {
        User user = BusinessLogic.GetUserByUserId(WebSecurity.CurrentUserId);
        return View(user);
    }

Stepping through, the model is populated but the CommunityId is null (which should be fine)

            @(Model.CommunityId.HasValue
                ? "COMMUNITYID"
                : "Not set!"
             )

Now I get:

Cannot perform runtime binding on a null reference

Is your BusinessLogic.GetUserByUserId function possibly returning a null, perhaps it doesn't recognize the user ID? If you're using the VS debugger, you should be able to examine the state of the variables, or possibly split them up into separate lines to make debugging easier.

If BusinessLogic.GetUserByUserId(WebSecurity.CurrentUserId) failes to return an object, for example WebSecurity.CurrentUserId returns zero, then .CommunityId will throw an exception (aka you're trying to access the Community Id property on an object that is NULL).

Breaking this up into multipe lines like the following will make it easier to determine where things go wrong:

var user = BusinessLogic.GetUserByUserId(WebSecurity.CurrentUserId);
if (user != null)
{
    return user.CommunityId.HasValue? "COMMUNITYID" : "Not set!";
}

// throw an ArgumentNullException (or something similiar) so that you know what happened
throw new ArgumentNullExpception("user);

问题是,在视图中的另一段代码中,我试图访问空值,而调试器错误地指出了错误的错误位置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM