0

The problem is that when I hover a mouse over the db.Reviews.ToList(); to see if data is populated correctly or not I see red cross warnings inside the box and then right after stepping over null referrence exception is thrown. This doesn't happen and everything works fine if I don't check what's inside. Why is that happening? Do I retrieve the data in an improper way?

    private DataContext db;
    public HomeController()
    {
        db = new DataContext();
    }

    public ActionResult ProductView (int? id)
    {
        if (id != null)
        {
            var book = Details(id);
            book.reviews = db.Reviews.ToList();
            return View(book);
        }
        else
           return HttpNotFound();
    }

Details method is just a bridge method that more than one Action methods use

public HomeModel Details(int? id)
        {
            HomeModel model = new HomeModel();
            var book = db.Books.Where(b => b.Id == id).Include(b => b.Author).Include(b=> b.Category.Books).FirstOrDefault();
            if (book == null)
            {
                HttpNotFound();
            }


            book.DisplayNumber++;
            db.SaveChanges();
            model.bookDetails = book;
            return model;
        }

HomeModel class is a wrapper that contains multiple classes to use in View:

public class HomeModel:ReviewModel
    {
        public List<BookModel> PopularBooks { get; set; }
        public List<BookModel> BestSales { get; set; }
         public List<Review> reviews { get; set; }
        public Book bookDetails { get; set; }

    }

The line that exception occured:

Line 101:            {
Line 102:                var book = Details(id);
Line 103:                **book.reviews = db.Reviews.ToList();**
Line 104:                return View(book);
Line 105:            }
Cavid
  • 125
  • 13
  • So you try debugging it step by step and it throws an error. But when you dont debug it works fine? – Narendran Pandian Apr 12 '19 at 10:14
  • I'm not sure how is this question a duplicate of that question lol. – Haytam Apr 12 '19 at 10:21
  • That logic seems strange. Do all your books share the same reviews? – Cleptus Apr 12 '19 at 10:22
  • @Haytam an exact duplicate. The OP hasn't posted any information, so the advice in that exact duplicate is the only way to troubleshoot this. Debug the code. Inspect the call stack. Check the variables – Panagiotis Kanavos Apr 12 '19 at 10:22
  • `The problem is that when I hover a mouse over the db.Reviews.ToList(); to see if data is populated correctly or not I see red cross warnings inside the box and then right after stepping over null referrence exception is thrown.`? He explained exactly where the problem occurs yet you say he didn't give any information. – Haytam Apr 12 '19 at 10:24
  • 1
    @Haytam that'sa not an explanation. It's not code, it's not an exception text. It doesn't show where the exception occured. The duplicate explains how to troubleshoot such problems. Besides, why do you assume that is relevant *at all*? What if `book` is null? – Panagiotis Kanavos Apr 12 '19 at 10:26
  • @ Panagiotis Kanavos what kind of info do you need more? If I dont get any error without checking the code at a runtime that means everything else works well, there's nothing to do with `Detail(id)` method. – Cavid Apr 12 '19 at 10:28
  • It's relevant because seeing red cross warnings when you hover on something where you have a LINQ query is totally normal because VS can't debug LINQ/Lambda expressions at runtime. Furthermore, the fact that if he doesn't hover on the thing doesn't throw the exception means that `book` cannot be null. – Haytam Apr 12 '19 at 10:29

1 Answers1

1

Do I retrieve the data in an improper way?

Nope! Your query is a very simple a normal one, you're just retrieving the list of reviews using a ToList().

Why is that happening?

You cannot debug LINQ queries at runtime with Visual Studio, that's why you're seeing crossed red things when you put your mouse on them, it's because you can't run them like that.

If you want to see the list of reviews, put it in a variable and look at it or place your mouse on db.Reviews and click View results (don't remember the exact thing).

Haytam
  • 4,643
  • 2
  • 20
  • 43
  • "cannot debug" is not a particularly helpful answer, i'd say. Do you know why is that happening? – Andrei Apr 12 '19 at 10:16
  • It *is* improper - a class-level context, no `using` block, no disposal. It's not possible to guess where the NRE occurs though as the OP hasn't posted enough code. Most likely it's in the `Detail()` method – Panagiotis Kanavos Apr 12 '19 at 10:17
  • 1
    The OP explicitly says that the NRE occurs when he hovers his mouse on the `db.Reviews.ToList()` and see red cross warnings, that goes well with the fact that we can't debug LINQ/Lambda expressions at runtime in Visual Studio. – Haytam Apr 12 '19 at 10:19
  • you can debug LINQ querries at runtime in a very convinient way with [OzCode](https://oz-code.com/). I'm not connected to them, but I love the product – Krzysztof Skowronek Apr 12 '19 at 10:19
  • @Panagiotis Kanavos As I said it works fine, I get all the data if I dont hover the mouse on `db.Reviews.ToList();` – Cavid Apr 12 '19 at 10:20
  • @Cavid you haven't posted enough code to reproduce the problem. Not even the exception text, just the message. An exception's call stack shows *where* the exception occured and which calls lead to it. You can get the full exception text easily with `Exception.ToString()` – Panagiotis Kanavos Apr 12 '19 at 10:24
  • @Cavid what does `Detail()` do ? What is `book`'s value? If it's null, you'll get an NRE right after `ToList()` returns the data. The exception's call stack would show that the exception was thrown in `ProductView` in that case. Or it could be the `reviews` setter that throws, because it tries to access a null field. In that case the call stack would show that the exception was thrown by that setter – Panagiotis Kanavos Apr 12 '19 at 10:27
  • @Panagiotis Kanavos edited the question right now.. I just tried to keep it brief as answerers find it hard to read long posted questions. hopefully you'll have detailed info from now on – Cavid Apr 12 '19 at 10:59
  • 1
    @Cavid, no, the full exception is still missing. In fact, the way `Details` is written, if the book isn't found it will throw an .... NRE, because it *doesn't* return if `book` is null – Panagiotis Kanavos Apr 12 '19 at 11:03
  • @Cavid we are going through the steps in the duplicate question right now. You'd find the answer a lot faster if you followed them. – Panagiotis Kanavos Apr 12 '19 at 11:05
  • @Panagiotis Kanavos what do you mean by "full exception" it's just `Null Referrence Exception : Object reference not set to an instance of an object.` – Cavid Apr 12 '19 at 11:08
  • @Cavid. No, that's just a message. It's not even the exception name. Definitely not the call stack. As I already explained, just call `Exception.ToString()` and post *that*. In the debugger you can see that information if you click on `View Details` in the exception dialog, or if you just expand the exception in the watch window. – Panagiotis Kanavos Apr 12 '19 at 11:12
  • @Cavid if you just let your application continue you'll get a yellow screen with the exception *and* the call stack in the browser – Panagiotis Kanavos Apr 12 '19 at 11:13