我如何解决System.NullReferenceException错误

时间:2016-07-22 09:51:41

标签: c# nullreferenceexception

我只是尝试创建一个WCF来获取所有客户端详细信息。当我尝试运行从SP获取数据的WCF时,它会显示以下错误:

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 个答案:

答案 0 :(得分:2)

根据您发布的代码,我建议您忘记创建CommanCall的实例。字段Comman是引用类型,默认情况下使用null进行初始化。因此,当您尝试呼叫NullReferenceException的成员时,会引发null。为Comman创建一个实例,例如:

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

如果字段Comman在其他地方初始化,请显示您捕获的异常堆栈跟踪。