Hello Team,
I encounter Exception error on the image path when saving data into the database, which says Object reference not set to an instance of the object, NOTE: I created the RoomImages folder in the repository folder. thanks
public ActionResult saveRoom(RoomViewModel objRoomViewModel)
{
string message = String.Empty;
string ImageUniqueName = String.Empty;
string ActualImageName = String.Empty;
if (objRoomViewModel.RoomId == 0)
{
ImageUniqueName = Guid.NewGuid().ToString();
ActualImageName = ImageUniqueName + Path.GetExtension(objRoomViewModel.Image.FileName);
objRoomViewModel.Image.SaveAs(filename: Server.MapPath("~/Repositories/RoomImages/" + ActualImageName));
Room objRoom = new Room()
{
RoomNumber = objRoomViewModel.RoomNumber,
RoomDescription = objRoomViewModel.RoomDescription,
RoomPrice = objRoomViewModel.RoomPrice,
BookingStatusId = objRoomViewModel.BookingStatusId,
IsActive = true,
RoomImage = ActualImageName,
RoomCapacity = objRoomViewModel.RoomCapacity,
RoomTypeId = objRoomViewModel.RoomTypeId
};
objHotelDbEntities.Rooms.Add(objRoom);
return Json(new {message = "Room successfully Updated.", success = true }, JsonRequestBehavior.AllowGet);
}
else
{
Room objRoom = objHotelDbEntities.Rooms.Single(model => model.RoomId == objRoomViewModel.RoomId);
if (objRoomViewModel.Image != null)
{
ImageUniqueName = Guid.NewGuid().ToString();
ActualImageName = ImageUniqueName + Path.GetExtension(objRoomViewModel.Image.FileName);
objRoomViewModel.Image.SaveAs(filename: Server.MapPath("~/RoomImages/" + ActualImageName));
objRoom.RoomImage = ActualImageName;
}
objRoom.RoomNumber = objRoomViewModel.RoomNumber;
objRoom.RoomDescription = objRoomViewModel.RoomDescription;
objRoom.RoomPrice = objRoomViewModel.RoomPrice;
objRoom.BookingStatusId = objRoomViewModel.BookingStatusId;
objRoom.IsActive = true;
objRoom.RoomCapacity = objRoomViewModel.RoomCapacity;
objRoom.RoomTypeId = objRoomViewModel.RoomTypeId;
message = "Updated successfully";
}
objHotelDbEntities.SaveChanges();
return Json(new { message = "Room successfully saved." + message, success = true }, JsonRequestBehavior.AllowGet);
}

Sam HobbsPosted Mar 21, 2024, 5:45 PM
The debugger is telling you that something is null. When you get this error and while in the debugger you can use the debugger to see what is null. The debugger seems to be pointing to
objRoomViewModel.Image. We know thatobjRoomViewModelis not null becauseobjRoomViewModel.RoomIdwas used successfully previously. I see later on in the code isif (objRoomViewModel.Image != null). The program should do that sooner. You need to determine whyImageinobjRoomViewModelis null.Emmmanuel FIADUFEPosted Mar 26, 2024, 1:15 PM
Thank you team, it is working now
Jignesh KumarPosted Mar 22, 2024, 5:03 AM
Hello,
You are trying to access on null Image, You need to check on this you should get image from your file upload control when you are trying to save first time, so you need to change in this line,
Vinoth XavierPosted Mar 22, 2024, 4:49 AM
Hi Emmmanuel,
Please verify the parent object is not null, before accessing thier child property.
Here
objRoomViewModel.Image will be null, that is the reason you got the exception.
Could you please add the condtion before tha like below,
Thanks.
Emmmanuel FIADUFEPosted Mar 21, 2024, 7:00 PM
Hello Sam,
I will check and revert please