本文介绍了在创建databasehelper对象时,C#中出现null异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

An exception of type 'System.NullReferenceException' occurred in Application.DataAccess.dll but was not handled in user code





我尝试过:



object [] parameters = null;



参数=新对象[4];

recordCount = 0;



参数[0] = pageIndex;

参数[1] = pageSize;

参数[2] = BuildWhereClause(filterKeys);

参数[ 3] = sort;



使用(DatabaseHelper dbHelper = new DatabaseHelper())

{

dbHelper。 Command.CommandTimeout = 300;



dsTaskDetails = dbHelper.ExecuteDataSet(usp_ins_GetValuationBasis,参数);



if(dsTaskDetails!= null&& dsTaskDetails.Tables.Count> 0)

if(dsTaskDetails.Tables [1]!= null)

recordCount = Convert.ToInt32(dsTaskDetails.Tables [1] .Rows [0] [RecordCount ]);

}



返回dsTaskDetails;



What I have tried:

object[] parameters = null;

parameters = new object[4];
recordCount = 0;

parameters[0] = pageIndex;
parameters[1] = pageSize;
parameters[2] = BuildWhereClause(filterKeys);
parameters[3] = sort;

using (DatabaseHelper dbHelper = new DatabaseHelper())
{
dbHelper.Command.CommandTimeout = 300;

dsTaskDetails = dbHelper.ExecuteDataSet("usp_ins_GetValuationBasis", parameters);

if (dsTaskDetails != null && dsTaskDetails.Tables.Count > 0)
if (dsTaskDetails.Tables[1] != null)
recordCount = Convert.ToInt32(dsTaskDetails.Tables[1].Rows[0]["RecordCount"]);
}

return dsTaskDetails;

推荐答案

if (dsTaskDetails != null && dsTaskDetails.Tables.Count > 0)
    if (dsTaskDetails.Tables[1] != null)
        recordCount = Convert.ToInt32(dsTaskDetails.Tables[1].Rows[0]["RecordCount"]);



dsTaskDetails.Tables.Count 时是一个, dsTaskDetails.Tables [1] 是一个无效的访问,它在检查 NULL 之前抛出一个execption。



因此,如果你真的想要访问第二个表,你应该检查一个大于1的表计数;如果你错误地使用了索引1,你应该使用索引0(注意:数组索引从零开始。


When dsTaskDetails.Tables.Count is one, dsTaskDetails.Tables[1] is an invalid access that throws an execption before checking for NULL.

So you should check for a table count greater than one if you really want to access the second table or use index 0 if you used index 1 by mistake (NOTE: Array indexes start at zero).


这篇关于在创建databasehelper对象时,C#中出现null异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 03:50