C
C#2w ago
Al3mid3x

✅ NullReferenceException for a array of bytes (image)

hey everybody i'm new to ASP.NET i was doing a small ecommerce project and i want to add a default picture for the product if not exists but i keep getting null error on the if statement that checks if its null. here is a picture of the error keeps appearing in visual studio
No description
9 Replies
canton7
canton72w ago
Model is null
Al3mid3x
Al3mid3x2w ago
well i'm trying to create a new product (which is the model used for this page)
@model Giftos.Entities.Product
@model Giftos.Entities.Product
and here is my Product class
c#
using System.ComponentModel.DataAnnotations;

namespace Giftos.Entities
{
public class Product
{
[Key]
public Guid Id { get; set; }

[Required(ErrorMessage = "This field is required.")]
public string Name { get; set; }

[Required(ErrorMessage = "This field is required.")]
public string Description { get; set; }

[Required(ErrorMessage = "This field is required.")]
public int Stock { get; set; }

[Required(ErrorMessage = "This field is required.")]
public int Price { get; set; }
public byte[] Image { get; set; }




public Product(string name, string description, int price, int stock)
{
Name = name;
Description = description;
Price = price;
Stock = stock;
}

public Product() { }
}
}
c#
using System.ComponentModel.DataAnnotations;

namespace Giftos.Entities
{
public class Product
{
[Key]
public Guid Id { get; set; }

[Required(ErrorMessage = "This field is required.")]
public string Name { get; set; }

[Required(ErrorMessage = "This field is required.")]
public string Description { get; set; }

[Required(ErrorMessage = "This field is required.")]
public int Stock { get; set; }

[Required(ErrorMessage = "This field is required.")]
public int Price { get; set; }
public byte[] Image { get; set; }




public Product(string name, string description, int price, int stock)
{
Name = name;
Description = description;
Price = price;
Stock = stock;
}

public Product() { }
}
}
Al3mid3x
Al3mid3x2w ago
ProductsController class
Kouhai
Kouhai2w ago
You need to provide a model instance to the view
Al3mid3x
Al3mid3x2w ago
can you tell me how to do it please as i said i'm new to asp.net
Kouhai
Kouhai2w ago
// GET: Products/Create
public IActionResult Create()
{
return View(new Product());
}
// GET: Products/Create
public IActionResult Create()
{
return View(new Product());
}
Should do the trick
Al3mid3x
Al3mid3x2w ago
Ye it did thank you i have another thing i need help with should i open another post for it ?@canton7@Kouhai
canton7
canton72w ago
Yes $close
MODiX
MODiX2w ago
If you have no further questions, please use /close to mark the forum thread as answered