InvalidOperationException Web Application

Viewed 34

I'm not very familiar with how this works, but I'm trying to submit and calculate the entered amounts for either deposit or withdraw, but now I'm getting this error: InvalidOperationException: Nullable object must have a value.

The entire error

I feel like I need a fresh set of eyes on this because I'm mentally stuck.

BankAppModel.cs:

using System.ComponentModel.DataAnnotations;

namespace Proj1BankApp.Models
{
    public class BankAppModel
    {
        [Required(ErrorMessage = "Please enter a name.")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Please enter a transaction month.")]
        public string TransactionMonth { get; set; }

        [Required(ErrorMessage = "Please enter a transaction day.")]
        [Range(1, 31, ErrorMessage = "The transaction day must be between 1 and 31.")]
        public int TransactionDay { get; set; }

        [Required(ErrorMessage = "Please enter a transaction year.")]
        public int TransactionYear { get; set; }

        public decimal? Balance { get; set; }

        public decimal? WithdrawAmount { get; set; }

        public decimal? DepositAmount { get; set; }

        public decimal Deposit()
        {
            decimal balance = 0;
            balance = Balance.Value + DepositAmount.Value;
            return balance;
        }

        public decimal Withdraw()
        {
            decimal balance = 0;
            if(Balance.Value < WithdrawAmount.Value)
            {
                return balance;
            }
            else
            {
                balance = balance - WithdrawAmount.Value;
                return balance;
            }
        }
    }
}

HomeController.cs:

    using Microsoft.AspNetCore.Mvc;
using Proj1BankApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Proj1BankApp.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public IActionResult Index()
        {
            ViewBag.BankApp = "";
            return View();
        }

        [HttpPost]
        public IActionResult Index(BankAppModel obj, string submit)
        {
            if (ModelState.IsValid)
            {
                ViewBag.BankApp = obj.Deposit().ToString("c2");
                ViewBag.BankApp = obj.Withdraw().ToString("c2");
            }
            else
            {
                ViewBag.BankApp = "";
            }
            return View();
        }
    }
}
2 Answers

Please make sure your nullable field has value before call its value property.

I assume that inside your Deposit method has Balance or DepositAmount null value. Try to put breakpoint there and check if these properties have null value assigned.

Or you can add null check like (in the same way inside Withdraw method):

public decimal Deposit()
{
     decimal balance = 0;
     if(Balance.HasValue && DepositAmount.HasValue)
     {
        balance = Balance.Value + DepositAmount.Value;
     }
     return balance;
}
Related