0

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

3 Answers 3

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

If BusinessLogic.GetUserByUserId(WebSecurity.CurrentUserId) failes to return an object, for example WebSecurity.CurrentUserId returns zero, then .CommunityId will throw an exception (a.k.a. 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);

Comments

0

The problem was that in a different piece of code in the view, I was trying to trying to access a null value and the debugger erroneously pointed to the wrong location of error.

1 Comment

Which would explain why I couldn't recreate the exception :). You should except this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.