【问题标题】:Object reference not set to an instance of an object in view model对象引用未设置为视图模型中的对象实例
【发布时间】:2013-05-01 17:11:27
【问题描述】:

我收到此错误:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error: 
Line 251:            ManageAccount.Instance.GetUserAccounts(username), "accountID", "name", accountFrom);
Line 252:
Line 253:            Currencies = new SelectList(
Line 254:            ManageCurrency.Instance.getCurrencies(), "id", "name",_Account.currency1.id);
Line 255:

它给出了这个 viewModel 中的错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Common;
using Business;
using System.Web.Mvc;
using System.Web.Security;
using System.Runtime.Remoting.Contexts;

namespace internetBankingApplication.ViewModel
{

    public class NewFixedAccountViewModel
    {
        private account _Account { get; set; }

        private fixedAccount _FixedAccount { get; set; }



        public SelectList AccountTypes { get; set; }

        public SelectList Durations { get; set; }

        public SelectList AccountFromList { get; set; }

        public SelectList Currencies { get; set; }

        public int ID
        {
            get
            {
                return _Account.accountID;
            }
        }

        [Required]
        [Display(Name = "Account Name")]
        public string Name
        {
            get
            {
                return _Account.name;
            }
            set
            {
                _Account.name = value;
            }
        }

        [Required]
        [Display(Name = "Account From")]
        public int accountFrom { get;set;}



        public string AccountFromName
        {
            get
            {
                string result = string.Empty;
                try
                {
                    result = ManageAccount.Instance.GetAccountBYID(accountFrom).name;
                }
                catch { }
                return result;
            }
        }



        [Required]
        [Display(Name = "Duration")]
        public int duration
        {
            get
            {
                return _FixedAccount.duration;
            }
            set
            {
                _FixedAccount.duration = value;
            }
        }

        public string durationName
        {
            get
            {
                string result = string.Empty;
                try
                {
                    result = ManageDuration.Instance.GetDurationById(_FixedAccount.duration).duration1;
                }
                catch { }
                return result;
            }
        }

        [Required]
        [Display(Name = "Available Balance")]
        public decimal AvailableBalance
        {
            get
            {
                return _Account.availableBalance;
            }
            set
            {
                _Account.availableBalance = value;
            }
        }

        [Required]
        [Display(Name = "Currency")]
        public int currency
        {
            get
            {
                return _Account.currency;
            }
            set
            {
                _Account.currency = value;
            }
        }

        public string CurrencyName
        {
            get
            {
                string result = string.Empty;
                try
                {
                    result = ManageCurrency.Instance.getTypesByID(_Account.currency).name;
                }
                catch { }
                return result;
            }
        }


        [Required]
        [Display(Name = "Account Description")]
        public string Description
        {
            get
            {
                return _Account.description;
            }
            set
            {
                _Account.description = value;
            }
        }


        [Required]
        [Display(Name = "Account Renew")]
        public bool renew
        {
            get
            {
                if (_FixedAccount.renew == 0)
                {
                    return false;
                }
                return true;
            }
            set
            {
                if (value == false)
                {
                    _FixedAccount.renew = 0;
                }
                else
                {

                    _FixedAccount.renew = 1;
                }
            }
        }


        public NewFixedAccountViewModel()
        {
           // AccountTypes = new SelectList(
           //ManageAccountType.Instance.getTypes(), "id", "type", _Account.typeID);

            string username = HttpContext.Current.User.Identity.Name.ToString();
            AccountFromList = new SelectList(
            ManageAccount.Instance.GetUserAccounts(username), "accountID", "name", accountFrom);

            **Currencies = new SelectList(
            ManageCurrency.Instance.getCurrencies(), "id", "name",_Account.currency1.id);** //Giving the error

            Durations = new SelectList(
            ManageDuration.Instance.GetAllDurations(), "id", "duration", _FixedAccount.duration);



        }

        public NewFixedAccountViewModel(string username)
        {
            AccountTypes = new SelectList(
            ManageAccountType.Instance.getTypes(), "id", "type", _Account.typeID);


            AccountFromList = new SelectList(
            ManageAccount.Instance.GetUserAccounts(username), "accountID", "name", accountFrom);

            Currencies = new SelectList(
            ManageCurrency.Instance.getCurrencies(), "id", "name", _Account.currency);

            Durations = new SelectList(
            ManageDuration.Instance.GetAllDurations(), "id", "duration", _FixedAccount.duration);


        }

        public NewFixedAccountViewModel(int accountID, string username)
        {
            _Account = ManageAccount.Instance.GetAccountBYID(accountID);

            _FixedAccount = ManageFixedAccount.Instance.GetFixedAccountByID(accountID);

            AccountFromList = new SelectList(
            ManageAccount.Instance.GetUserAccounts(username), "accountID", "name", accountFrom);

            AccountTypes = new SelectList(
            ManageAccountType.Instance.getTypes(), "id", "type", _Account.accountType);

            Currencies = new SelectList(
            ManageCurrency.Instance.getCurrencies(), "id", "name", _Account.currency);

            Durations = new SelectList(
            ManageDuration.Instance.GetAllDurations(), "id", "duration", _FixedAccount.duration);


        }
    }
}

【问题讨论】:

    标签: asp.net-mvc-3 razor asp.net-mvc-viewmodel


    【解决方案1】:

    您收到的错误消息意味着您正在尝试在空值上调用方法或属性 - 这是您无法做到的。

    据我所知,ManageCurrency 为 null 或 ManageCurrency.Instance 为 null。

    如果ManageCurrency 为空,则对属性Instance 的调用将失败,因为空对象没有方法或属性。同样,如果ManageCurrency.Instance 为空,则对方法getCurrencies 的调用会因同样的原因而失败。

    无论如何 - 这并不难调试。正如 skumar 建议的那样,在该行设置一个断点,在调试模式下运行,然后查看 null 值在哪里,然后向后工作以找出该值为 null 的原因。

    【讨论】:

      【解决方案2】:

      您在以下代码中传递的用户名可能是空的或无效的。这就是它返回 NULL 并发生上述异常的原因。

      AccountFromList = new SelectList(
                  ManageAccount.Instance.GetUserAccounts(username), "accountID", "name", accountFrom);
      

      【讨论】:

      • 不,我查了一下..它工作正常..问题是关于货币的
      • 在货币行中放置一个断点并在调试模式下运行您的程序。在它命中断点后检查相应的对象,是否有任何对象为 NULL。
      猜你喜欢
      • 2020-03-05
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多