2016-07-22 15 views
0

すべてのクライアントの詳細を取得するためにWCFの1つを作成しようとします。System.NullReferenceExceptionエラーを解決する方法

service error

キャッチされた例外:

caught exception

も入れブレークポイント私はIDを参照してくださいその時私はSPからのデータのショーこのエラーが出ることWCFを実行しようとすると来てもまだ同じエラーを表示しています。

クラスコード:

public class CommanCall 
{ 
    string Connection = "Data Source=USER-PC\\SQLEXPRESS;Initial Catalog=BlueEyeNewDatabase;Integrated Security=True"; 

    public List<Client> SelectAllClient(int id) 
    { 
     List<Client> ClientList = new List<Client>(); 
     using (var Context = new EmpSystemContext(Connection)) 
     { 
      var DbResult = Context.SelectClientDetails(id); 
      if (DbResult != null) 
      { 
       foreach (var Row in DbResult) 
       { 
        Client clist = new Client 
        { 
         ClientName = Row.ClientName, 
         ClientAddress = Row.ClientAddress, 
         PreferredCurrency = Row.PreferredCurrency, 
         FirstName = Row.FirstName, 
         LastName = Row.LastName, 
         City = Row.City, 
         State = Row.State, 
         Country = Row.Country, 
         PostalCode = Row.PostalCode, 
         ContactName = Row.ContactName, 
         ContactNumber = Row.ContactNumber, 
         Email = Row.Email, 
         ContactEmail = Row.ContactEmail 
        }; 
        ClientList.Add(clist); 
       } 
      } 
     }      
     return ClientList; 
    } 
} 

Service.svc.cs

public class Service1 : IService1 
{ 
    public static EmpSystem.Domain.CommanCall Comman; 

    public ListResponce<Client> GetAllClientDetailsById(int id) 
    { 
     ListResponce<Client> lstclientResp = new ListResponce<Client>(); 
     lstclientResp.Message = "Taru kai na thai ek record find na thayo"; 
     lstclientResp.Success = false; 
     int id1 = id; 
     List<Client> lstclient = Comman.SelectAllClient(id); 
     lstclientResp.Result = lstclient; 
     if(lstclient!=null) 
     { 
      lstclientResp.Message = "Congo hahahhah Record Find thaya"; 
      lstclientResp.Success = true; 
     } 
     return new ListResponce<Client> 
     { 
      Message = lstclientResp.Message, 
      Success = lstclientResp.Success, 
      Result = lstclientResp.Result 
     };   
    }  
} 

IServiceあなたは、私がお勧めすることができ掲示コードから

public interface IService1 
{ 
    [OperationContract] 
    [System.ServiceModel.Web.WebInvoke(Method = "GET", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json, BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped)] 
    ListResponce<Client> GetAllClientDetailsById(int id); 
} 
+1

[NullReferenceExceptionとは何か、それを修正するにはどうすればいいですか?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix -it) – CompuChip

答えて

2

ファイルは、あなたがのインスタンスを作成するのを忘れCommanCall。フィールドCommanは、デフォルトでnullで初期化される参照タイプです。したがってnullのメンバーを呼び出そうとするとNullReferenceExceptionがスローされます。例えば、Commanのインスタンスを作成します。

public static EmpSystem.Domain.CommanCall Comman = new EmpSystem.Domain.CommanCall(); 

フィールドCommanがどこかに初期化されている場合、あなたがキャッチした例外のスタックトレースを表示してください。

+0

ありがとうございました – Herry

関連する問題