2016-07-22 371 views
0

我只是尝试创建一个WCF来获取所有客户端详细信息。当我尝试运行WCF其得到SP数据的显示此错误:如何解决System.NullReferenceException错误

service error

捕捉到异常:

caught exception

而且也把当破发点那个时候,我看到的ID即将到来,但仍显示相同的错误。

类代码:

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-doi-i-fix -it) – CompuChip

回答

2

从你贴我可以建议你的代码忘记了创建的实例CommanCall。字段Comman是默认初始化为null的引用类型。所以NullReferenceException当你试图调用null成员时抛出。对于Comman创建一个实例,例如:

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

如果场Comman初始化别的地方,请,异常的显示堆栈跟踪你抓住。

+0

现在再次感谢你的工作,再次感谢你 – Herry