使用语句内部的NullReferenceException [英] NullReferenceException inside using statement

查看:68
本文介绍了使用语句内部的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在我的方法中我正在使用using语句进行数据访问。

在此之前,我创建了一个对象类。这很好用。


在using语句中我设置了类的属性(数据来自

数据库)。

获取数据。


设置数据不起作用。它说'是:

System.NullReferenceException:对象引用未设置为

对象的实例。


什么我该怎么办?


谢谢!

Hi,

Inside my method I''m using the using-statement for data access.
Before that, I create an object of a class. This works fine.

Inside the using-statement I set properties of the class (with the data from
the database).
Getting the data works.

Setting the data doesn''t work. It say''s:
System.NullReferenceException: Object reference not set to an instance of an
object.

What should I do?

Thanks!

推荐答案

Arjen写道:
Arjen wrote:
我该怎么办?




请告诉我们相关的代码。

Oliver Sturm

-

omnibus ex nihibo ducendis sufficit unum

插入空格以防止谷歌电子邮件销毁:

MSN oliver @ sturmnet.org Jabber sturm @ amessage.de

ICQ 27142619 http://www.sturmnet.org / blog


Arjen写道:
Arjen wrote:
我该怎么办?




请告诉我们相关的代码。

Oliver Sturm

-

omnibus ex nihilo ducendis sufficit unum

空格插入以防止谷歌电子邮件销毁:

MSN oliver @ sturmnet.org Jabber sturm @ amessage.de

ICQ 27142619 http://www.sturmnet.org/blog


这是:


public static void LoadPersons(字符串名称)

{

//创建人

人员myPerson = new Person();


//创建CacheManager对象。

CacheManager primitivesCache = CacheFactory.GetCacheManager(" Person Cache

Manager");


//检查所请求对象的缓存

myPerson =(Person)primitivesCache.GetData(name);


if(myPerson == null)

{

//请求的对象未缓存;因此,从

//数据提供程序中检索它并将其缓存以获得更多请求。


//读取

使用( IDataReader personDataReader = Bll.PersonSelect())

{

试试

{

while(personDataReader.Read( ))

{

//从DataReader获取值

myPerson.Id =(int)personDataReader [" Id"];

myPerson.Name =(string)personDataReader [" Name"];

myPerson.Address =(string)personDataReader [" Address"];

}

}

catch(例外情况)

{

throw(ex); < br $>
}

}

// ...

}

//。 ..

}


通过这一行我收到错误:

myPerson.Id =(int)personDataReader [" Id"];


希望这会有所帮助!
Here it is:

public static void LoadPersons(string name)
{
// Create person
Persons myPerson = new Person();

// Create the CacheManager object.
CacheManager primitivesCache = CacheFactory.GetCacheManager("Person Cache
Manager");

// Check cache for requested object
myPerson = (Person)primitivesCache.GetData(name);

if (myPerson == null)
{
// Requested object is not cached; therefore, retrieve it from
// the data provider and cache it for more requests.

// Read
using (IDataReader personDataReader = Bll.PersonSelect())
{
try
{
while (personDataReader.Read())
{
// Get the values from the DataReader
myPerson.Id = (int)personDataReader["Id"];
myPerson.Name = (string)personDataReader["Name"];
myPerson.Address = (string)personDataReader["Address"];
}
}
catch (Exception ex)
{
throw (ex);
}
}
//...
}
//...
}

By this line I get the error:
myPerson.Id = (int)personDataReader["Id"];

Hope this helps!


这篇关于使用语句内部的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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