0

Product when I put the breakpoint to the following code. You can jump into the insert line and get a null exception message.

public ActionResult Create([Bind(Include = "ID,Name,CategoryId,Brand,Model,ImageUrl,Description,Price, Tax,Discount,Stock, IsActive,CreateDate,CreateUserId,UpdateDate,UpdateUserId")] Product product, IEnumerable files)

if (files != null&&files.Count()>0)
            {

                foreach (var item in files)
                {


                    Tuple<string, string> paths = ImageUpload.SaveImageWithThumbnail(item, 329, 380, 84, 84, false);

                    product.Images.Add(new Images

                    {
                        ImageUrl = paths.Item1,

                        ImageUrlt = paths.Item2,
                        RegisterDate = DateTime.Now,
                        CreateDate = DateTime.Now,
                        CreateUserId = 1,
                        UpdateDate = DateTime.Now,
                        UpdateUserId = 1

                    });
                }
            }

            **db.Products.Add(product);**
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.CategoryId = new SelectList(db.Categories, "ID", "Name", product.CategoryId);
        return View(product);
    }
  • What line is being skipped and what line is throwing the error? [Popular StackOverFlow answer over null Exceptions](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Symon Jan 04 '19 at 20:27
  • Please try to ask your question in a more detailed way. Which line of code is problematic? What is the exact error message you're getting? – Rufus L Jan 04 '19 at 20:39
  • disables db.Product. (product) line and NullReferenceException exception error thank you – Yunus Ayhan Jan 04 '19 at 21:08
  • why do you think db.Products is not itself null? you didn't really post enough code here for us to help you... – Nikki9696 Jan 04 '19 at 21:41
  • 1
    Edit your post and copy paste your error message. Probably a guess that you do not initialize the Images collection in your Product class having seen a lot of people make that error including myself. – Andez Jan 04 '19 at 22:25
  • ** db.Products.Add (product); ** I get an error that has not been set to an instance of an object in the Object reference line, and I see it skipping this line when I watch it with breakpoint. NullReferenceException – Yunus Ayhan Jan 05 '19 at 07:40
  • 1
    Either db or products is still null when you want to add product. Btw, you can ommit &&files.Count()>0 – Hypenate Jan 05 '19 at 07:46
  • thank you but I didn't understand your answer – Yunus Ayhan Jan 05 '19 at 08:33

0 Answers0