0

I am unable to find the root cause of the error as this method is giving NullReferenceException-----

  public dynamic GetPosts()
    {

        var ret = (from post in db.Posts.ToList()
                   orderby post.PostedDate descending
                   select new
                   {
                       Message = post.Message,
                       PostedBy = post.PostedBy,
                      // PostedByName = post.UserProfile.UserName,
                       PostedByAvatar = imgFolder + (String.IsNullOrEmpty(post.UserProfile.AvatarExt) ? defaultAvatar : post.PostedBy + "." + post.UserProfile.AvatarExt),
                       PostedDate = post.PostedDate,
                       PostId = post.PostId,
                       PostComments = from comment in post.PostComments.ToList()
                                      orderby comment.CommentedDate
                                      select new
                                      {
                                          CommentedBy = comment.CommentedBy,
                                         // CommentedByName = comment.UserProfile.UserName,
                                          CommentedByAvatar = imgFolder + (String.IsNullOrEmpty(comment.UserProfile.AvatarExt) ? defaultAvatar : comment.CommentedBy + "." + comment.UserProfile.AvatarExt),
                                          CommentedDate = comment.CommentedDate,
                                          CommentId = comment.CommentId,
                                          Message = comment.Message,
                                          PostId = comment.PostId

                                      }
                   }).AsEnumerable();
        return ret;
    }

In the stack Trace, i am unable to find the route cause of the error as nothing detailed relative information is showing there.

My User table,and Post table and comment table are joined together and working fine if i try to post something on the view page. My PostMethod is working fine and is something like this-----

    public HttpResponseMessage PostPost(Post post)
    {
        post.PostedBy = WebSecurity.CurrentUserId;
        post.PostedDate = DateTime.UtcNow;
        ModelState.Remove("post.PostedBy");
        ModelState.Remove("post.PostedDate");
        if (ModelState.IsValid)
        {
            db.Posts.Add(post);
            db.SaveChanges();
            var usr = db.UserProfile.FirstOrDefault(x => x.UserId == post.PostedBy);
            var ret = new
            {
                Message = post.Message,
                PostedBy = post.PostedBy,
                PostedByName = usr.UserName,
                PostedByAvatar = imgFolder + (String.IsNullOrEmpty(usr.AvatarExt) ? defaultAvatar : post.PostedBy + "." + post.UserProfile.AvatarExt),
                PostedDate = post.PostedDate,
                PostId = post.PostId
            };
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, ret);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = post.PostId }));
            return response;
        }
        else
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
        }
     }

My User table is-----

        [Table("UserProfile")]
public class UserProfile
{
    public UserProfile()
    {
        this.PostComments = new HashSet<PostComment>();
        this.Posts = new HashSet<Post>();
    }
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string AvatarExt { get; set; }
    public virtual ICollection<PostComment> PostComments { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
}

and my Post table is-----

      public class Post
{
    public Post()
    {
        this.PostComments = new HashSet<PostComment>();
    }
    [Key]
    public int PostId { get; set; }
    public string Message { get; set; }
    public int PostedBy { get; set; }
    public System.DateTime PostedDate { get; set; }
    public int UserId { get; set; }

    public virtual ICollection<PostComment> PostComments { get; set; }
    public virtual UserProfile UserProfile { get; set; }
}

and my PostComment table is----

     public class PostComment
{

    [Key]
    public int CommentId { get; set; }
    public int PostId { get; set; }
    public string Message { get; set; }
    public int CommentedBy { get; set; }
    public System.DateTime CommentedDate { get; set; }
    public int UserId { get; set; }

    public virtual Post Post { get; set; }
    public virtual UserProfile UserProfile { get; set; }
}

I am following this article----- http://techbrij.com/facebook-wall-posts-comments-knockout-aspnet-webapi Plzz suggest me what should i do.

duke
  • 1,476
  • 1
  • 13
  • 24
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Oct 22 '15 at 22:35
  • @StephenMuecke the question is not duplicate. Same Exception are thrown in thousands of code that's not mean that all are of same type and all would be solved using only one reference given by u. Btw, i have resolved the issue by myself. I will upload the solution soon. Thank u – duke Oct 23 '15 at 17:06
  • It is a duplicate. The answers tells you why you get it and shows you how to debug your code to determine where the error is. You have not even indicated which line the error occurs on so no one can help you with this. Only you can debug it. The most obvious line of code is `String.IsNullOrEmpty(post.UserProfile.AvatarExt)` which would throw an exception is `UserProfile` is `null` –  Oct 23 '15 at 23:14

0 Answers0