对象引用未设置为对象的实例(我通过以下代码得到错误) [英] object reference not set to an instance of an object(i got an error through the below code)

查看:78
本文介绍了对象引用未设置为对象的实例(我通过以下代码得到错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 DataTable dt = ds.Tables [ 0 ]; 


HolidayReg objHoliday = new HolidayReg();

for int row = 0 ; row < dt.Rows.Count; row ++)
{
objHoliday = HolidayReg();
foreach (DataColumn dc in dt.Columns)
{
foreach (PropertyInfo prop in objHoliday.GetType()。GetProperties())
{

if (prop.Name == dc.ColumnName&& prop.CanWrite)
{
if (dt.Rows [row] [dc.ColumnName]!= DBNull.Value)
{

prop.SetValue(objHoliday,dt .Rows [row] [dc.ColumnName], null );
}
}
}
}


ListHoliday.Add(objHoliday);
}


}
catch (例外情况)
{
throw ex;
}

return ListHoliday;
}



i在catch时出错(Expression ex)

解决方案

1。)你捕获时没有错误。它发生在那之前并由catch块处理。

2.)它可能是代码中的任何位置。我们很难确定它可能存在的位置。它可以早在第一个声明中:

 ds.Tables [0]; 





其中ds可以为null。为了防止这种情况,您还应该检查对象是否为空,如下所示:



 DataTable dt; 
if (ds!= null
{
if (ds.Tables.count > 0
{
dt = ds.Tables [ 0 ];
}
}



3.)使用Visual Studio调试代码,您就会知道错误发生的位置。看看Abhijit Jana的精彩文章:

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]


实际上用这个小代码找不到错误是不可能的你发布但我的假设是你应该改变这一行

 prop.SetValue(objHoliday,dt.Rows [row] [dc.ColumnName], null ); 

 prop.SetValue(objHoliday,dt.Rows [row] [dc.ColumnName] .ToString(), null ); 


您的数据表中正在传递一些空值。使用快速监视检查它们

DataTable dt = ds.Tables[0];


                HolidayReg objHoliday = new HolidayReg();

                for (int row = 0; row < dt.Rows.Count; row++)
                {
                    objHoliday = new HolidayReg();
                    foreach (DataColumn dc in dt.Columns)
                    {
                        foreach (PropertyInfo prop in objHoliday.GetType().GetProperties())
                        {

                            if (prop.Name == dc.ColumnName && prop.CanWrite)
                            {
                                if (dt.Rows[row][dc.ColumnName] != DBNull.Value)
                                {

                                    prop.SetValue(objHoliday, dt.Rows[row][dc.ColumnName], null);
                                }
                            }
                        }
                    }


                    ListHoliday.Add(objHoliday);
                }


            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ListHoliday;
        }


i got an error at catch(Expression ex)

解决方案

1.) You didn''t get error at catch. It happened much before that and was handled by catch block.
2.) It could be any where in the code. It''s difficult for us to pin point where it could be. It could as early as in first statement:

ds.Tables[0];



where the ds could be null. To prevent that, you should also check if the object is not null like this:

DataTable dt;
if(ds != null)
{
    if(ds.Tables.count > 0)
    {
        dt = ds.Tables[0];
    }
}


3.) Use Visual Studio to debug the code, and you would know where the error occured. Look at the great article from Abhijit Jana here:
Mastering Debugging in Visual Studio 2010 - A Beginner''s Guide[^]


Actually it is not possible to find out the error with this little code you have posted but my assumption is you should change this line

prop.SetValue(objHoliday, dt.Rows[row][dc.ColumnName], null);

to

prop.SetValue(objHoliday, dt.Rows[row][dc.ColumnName].ToString(), null);


Some null value is being passeed in your data table..check it ones by using quick watch


这篇关于对象引用未设置为对象的实例(我通过以下代码得到错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆