如何解决 System.NullReferenceException 错误

How do I solve the System.NullReferenceException error

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

捕捉到异常:

而且当设置断点时我看到 ID 即将到来但仍然显示相同的错误。

Class代码:

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

I服务文件

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

根据您发布的代码,我可以建议您忘记创建 CommanCall 的实例。字段 Comman 是引用类型,默认情况下用 null 初始化。因此,当您尝试调用 null 的成员时抛出 NullReferenceException。为 Comman 创建实例,例如:

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

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