2013-05-01 30 views
0

我得到這個錯誤:對象引用未設置到視圖模型對象的實例

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: 

它給這個視圖模型

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); 


     } 
    } 
} 

回答

0

的錯誤,您正在傳入以下的用戶名代碼可能爲空或無效。這就是爲什麼它返回NULL併發生以上異常。

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

不,我檢查它..它工作正常..問題是關於貨幣 – 2013-05-01 18:11:36

+1

在貨幣行中放置一個斷點,並在DEBUG模式下運行您的程序。在它到達斷點後,檢查相應的對象,如果任何對象爲NULL或不是。 – 2013-05-01 18:20:11

1

你得到你試圖調用一個空值的方法或屬性手段的錯誤信息 - 你不能這樣做。

從我所看到的ManageCurrency爲空或ManageCurrency.Instance爲空。

如果ManageCurrency爲空,則對屬性Instance的調用失敗,因爲空對象沒有方法或屬性。同樣,如果ManageCurrency.Instance爲空,則由於相同的原因,對方法getCurrencies的調用失敗。

在任何情況下 - 這不難調試。正如skumar所建議的那樣,在該行上設置斷點,以調試模式運行並查看空值的位置,然後向後查找值爲空的原因。

相關問題