Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
执行方法时发生c#NullReferenceException_C#_Visual Studio 2015 - Fatal编程技术网

执行方法时发生c#NullReferenceException

执行方法时发生c#NullReferenceException,c#,visual-studio-2015,C#,Visual Studio 2015,我在此处执行此方法时出错: public void chercher_Employe(System.Data.DataSet Dset, String critere, out String erreur, out Boolean exist, out employe tab_emp) { exist = false; erreur = null; ////////// here wher

我在此处执行此方法时出错:

public void chercher_Employe(System.Data.DataSet Dset, String critere, out String erreur, out Boolean
        exist, out employe tab_emp)
        {

                exist = false;
                erreur = null;

    ////////// here where the error happens
                if (Dset.Tables["employe"].Rows.Count > 0)
                 {
                    ligne = Dset.Tables[table].Select(critere);
                    if (ligne.Count() > 0)
                    {
                        exist = true;
                        tab_emp = new employe();
                        tab_emp.num_e = Convert.ToInt32(ligne[0]["num_e"]);
                        tab_emp.nom_e = ligne[0]["nom_e"].ToString();
                        tab_emp.num_r = Convert.ToInt32(ligne[0]["num_r"]);
                        tab_emp.sal_e = Convert.ToInt32(ligne[0]["sal_e"]);
                        tab_emp.adr_e = ligne[0]["adr_e"].ToString();
                        tab_emp.nom_ser = ligne[0]["nom_ser"].ToString();
                    }
                    else
                    { exist = false; }
                }
                else erreur = "Table employé est vide";
            }
这就是我执行此方法时得到的结果:


提前感谢。

以下是需要重点关注的两行代码”

第1行正在根据您的查询调用表结果。如果没有任何内容与查询的条件匹配,则不会返回任何行;因此
ligne
的值为
null
。表==null没有
Count()
属性并引发异常。最简单的修复方法是更新第2行以捕获null:

if ((ligne != null) && (ligne.Count() > 0))

好的,
Dset
null
还是
Dset.Tables[“employe”]
返回一个
null
值。我们无法告诉您是哪一个,您需要自己弄清楚(使用调试器)您能解释更多吗?如果(Dset.Tables[“employe”].Rows.Count>0),那么我就会得到错误
if ((ligne != null) && (ligne.Count() > 0))