473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

NullReferenceException inside using statement

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!
Nov 17 '05 #1
19 3927
Arjen wrote:
What should I do?


Show us the relevant code, please.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #2
Arjen wrote:
What should I do?


Show us the relevant code, please.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #3
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!
Nov 17 '05 #4
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!
Nov 17 '05 #5
I think you missed including this line after you know myPerson is null...

myPerson = new Person();

And why you try reassign this null reference in a while-loop is beyond my
understanding... =)

Happy Coding
- Michael S

"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news3.zwoll1.ov.home.nl...
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!

Nov 17 '05 #6
I think you missed including this line after you know myPerson is null...

myPerson = new Person();

And why you try reassign this null reference in a while-loop is beyond my
understanding... =)

Happy Coding
- Michael S

"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news3.zwoll1.ov.home.nl...
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!

Nov 17 '05 #7
I don't see what your point is.
I start with "myPerson = new Person();", try to get it from the cache, if
there is no cache then it is null.
(With null I mean that the properties like name and id are not set.)

About the while, you are correct because there is one result.
(But I use the same code where I get back multiple records)

Arjen

"Michael S" <a@b.c> schreef in bericht
news:Op**************@TK2MSFTNGP12.phx.gbl...
I think you missed including this line after you know myPerson is null...

myPerson = new Person();

And why you try reassign this null reference in a while-loop is beyond my
understanding... =)

Happy Coding
- Michael S

"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news3.zwoll1.ov.home.nl...
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!


Nov 17 '05 #8
I'm leaving a few lines below in sequence while cutting out others.

Arjen wrote:
public static void LoadPersons(string name)
{
// Create person
Persons myPerson = new Person(); // Check cache for requested object
myPerson = (Person)primitivesCache.GetData(name); if (myPerson == null)
{ myPerson.Id = (int)personDataReader["Id"];


So, you wonder why you get a null reference exception? You sequence of
statements in this regard says:

1. Create a Person
2. Get a person from the cache (which apparently may be null)
3. Proceed to (4) only if the person actually is null
4. Access the current person (which is null, see (3)!)

I'm not going to take wild guesses at the purpose of the whole exercise,
but I guess it's obvious that the line you have right at the start of
the method, where a new Person is created, would really belong right at
the start of the block of code where you have made sure that you haven't
found a person in the cache (after the "if (myPerson == null) {" line).

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #9
I don't see what your point is.
I start with "myPerson = new Person();", try to get it from the cache, if
there is no cache then it is null.
(With null I mean that the properties like name and id are not set.)

About the while, you are correct because there is one result.
(But I use the same code where I get back multiple records)

Arjen

"Michael S" <a@b.c> schreef in bericht
news:Op**************@TK2MSFTNGP12.phx.gbl...
I think you missed including this line after you know myPerson is null...

myPerson = new Person();

And why you try reassign this null reference in a while-loop is beyond my
understanding... =)

Happy Coding
- Michael S

"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news3.zwoll1.ov.home.nl...
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!


Nov 17 '05 #10
I'm leaving a few lines below in sequence while cutting out others.

Arjen wrote:
public static void LoadPersons(string name)
{
// Create person
Persons myPerson = new Person(); // Check cache for requested object
myPerson = (Person)primitivesCache.GetData(name); if (myPerson == null)
{ myPerson.Id = (int)personDataReader["Id"];


So, you wonder why you get a null reference exception? You sequence of
statements in this regard says:

1. Create a Person
2. Get a person from the cache (which apparently may be null)
3. Proceed to (4) only if the person actually is null
4. Access the current person (which is null, see (3)!)

I'm not going to take wild guesses at the purpose of the whole exercise,
but I guess it's obvious that the line you have right at the start of
the method, where a new Person is created, would really belong right at
the start of the block of code where you have made sure that you haven't
found a person in the cache (after the "if (myPerson == null) {" line).

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #11
Arjen <bo*****@hotmail.com> wrote:
I don't see what your point is.
I start with "myPerson = new Person();", try to get it from the cache, if
there is no cache then it is null.
(With null I mean that the properties like name and id are not set.)


No, that's not what null means. It means you don't have a reference to
an object. Removing the using statement from the picture (because it's
irrelevant to your problem) you're doing:

if (myPerson==null)
{
myPerson.Id = (int)personDataReader["Id"];
...
}

That's trying to trying to set the property on a null reference, which
is why you're getting the exception.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #12
Arjen <bo*****@hotmail.com> wrote:
I don't see what your point is.
I start with "myPerson = new Person();", try to get it from the cache, if
there is no cache then it is null.
(With null I mean that the properties like name and id are not set.)


No, that's not what null means. It means you don't have a reference to
an object. Removing the using statement from the picture (because it's
irrelevant to your problem) you're doing:

if (myPerson==null)
{
myPerson.Id = (int)personDataReader["Id"];
...
}

That's trying to trying to set the property on a null reference, which
is why you're getting the exception.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #13
Have a look at Olivers answer.

At work I have a so called 'bug cup'. It's just a cup. Or sometimes a glass
or a mug.
But whenever I make silly mistakes, like you did; I have to give the cup a
coin.

My cup is almost always full! =)

- Michael S
"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news6.zwoll1.ov.home.nl...
I don't see what your point is.
I start with "myPerson = new Person();", try to get it from the cache, if
there is no cache then it is null.
(With null I mean that the properties like name and id are not set.)

About the while, you are correct because there is one result.
(But I use the same code where I get back multiple records)

Arjen

"Michael S" <a@b.c> schreef in bericht
news:Op**************@TK2MSFTNGP12.phx.gbl...
I think you missed including this line after you know myPerson is null...

myPerson = new Person();

And why you try reassign this null reference in a while-loop is beyond my
understanding... =)

Happy Coding
- Michael S

"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news3.zwoll1.ov.home.nl...
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!



Nov 17 '05 #14
Have a look at Olivers answer.

At work I have a so called 'bug cup'. It's just a cup. Or sometimes a glass
or a mug.
But whenever I make silly mistakes, like you did; I have to give the cup a
coin.

My cup is almost always full! =)

- Michael S
"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news6.zwoll1.ov.home.nl...
I don't see what your point is.
I start with "myPerson = new Person();", try to get it from the cache, if
there is no cache then it is null.
(With null I mean that the properties like name and id are not set.)

About the while, you are correct because there is one result.
(But I use the same code where I get back multiple records)

Arjen

"Michael S" <a@b.c> schreef in bericht
news:Op**************@TK2MSFTNGP12.phx.gbl...
I think you missed including this line after you know myPerson is null...

myPerson = new Person();

And why you try reassign this null reference in a while-loop is beyond my
understanding... =)

Happy Coding
- Michael S

"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news3.zwoll1.ov.home.nl...
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!



Nov 17 '05 #15
;-)

I see the problem (/solution) now.

Thanks!
Arjen

"Michael S" <a@b.c> schreef in bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
Have a look at Olivers answer.

At work I have a so called 'bug cup'. It's just a cup. Or sometimes a
glass or a mug.
But whenever I make silly mistakes, like you did; I have to give the cup a
coin.

My cup is almost always full! =)

- Michael S
"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news6.zwoll1.ov.home.nl...
I don't see what your point is.
I start with "myPerson = new Person();", try to get it from the cache, if
there is no cache then it is null.
(With null I mean that the properties like name and id are not set.)

About the while, you are correct because there is one result.
(But I use the same code where I get back multiple records)

Arjen

"Michael S" <a@b.c> schreef in bericht
news:Op**************@TK2MSFTNGP12.phx.gbl...
I think you missed including this line after you know myPerson is null...

myPerson = new Person();

And why you try reassign this null reference in a while-loop is beyond
my understanding... =)

Happy Coding
- Michael S

"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news3.zwoll1.ov.home.nl...
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!



Nov 17 '05 #16
;-)

I see the problem (/solution) now.

Thanks!
Arjen

"Michael S" <a@b.c> schreef in bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
Have a look at Olivers answer.

At work I have a so called 'bug cup'. It's just a cup. Or sometimes a
glass or a mug.
But whenever I make silly mistakes, like you did; I have to give the cup a
coin.

My cup is almost always full! =)

- Michael S
"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news6.zwoll1.ov.home.nl...
I don't see what your point is.
I start with "myPerson = new Person();", try to get it from the cache, if
there is no cache then it is null.
(With null I mean that the properties like name and id are not set.)

About the while, you are correct because there is one result.
(But I use the same code where I get back multiple records)

Arjen

"Michael S" <a@b.c> schreef in bericht
news:Op**************@TK2MSFTNGP12.phx.gbl...
I think you missed including this line after you know myPerson is null...

myPerson = new Person();

And why you try reassign this null reference in a while-loop is beyond
my understanding... =)

Happy Coding
- Michael S

"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news3.zwoll1.ov.home.nl...
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!



Nov 17 '05 #17
The problem being out of cups? =)

Enough of my taunts. What you really need to do is to learn about
reference-types and value-types and how classes and structs allocates on
heap and stack. And how they don't.

Jon Skeet, who posted above, have some excellent guides on that. Better than
any book...

Happy Reading
- Michael S

"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news3.zwoll1.ov.home.nl...
;-)

I see the problem (/solution) now.

Thanks!
Arjen

"Michael S" <a@b.c> schreef in bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
Have a look at Olivers answer.

At work I have a so called 'bug cup'. It's just a cup. Or sometimes a
glass or a mug.
But whenever I make silly mistakes, like you did; I have to give the cup
a coin.

My cup is almost always full! =)

- Michael S
"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news6.zwoll1.ov.home.nl...
I don't see what your point is.
I start with "myPerson = new Person();", try to get it from the cache,
if there is no cache then it is null.
(With null I mean that the properties like name and id are not set.)

About the while, you are correct because there is one result.
(But I use the same code where I get back multiple records)

Arjen

"Michael S" <a@b.c> schreef in bericht
news:Op**************@TK2MSFTNGP12.phx.gbl...
I think you missed including this line after you know myPerson is
null...

myPerson = new Person();

And why you try reassign this null reference in a while-loop is beyond
my understanding... =)

Happy Coding
- Michael S

"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news3.zwoll1.ov.home.nl...
> 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!
>



Nov 17 '05 #18
The problem being out of cups? =)

Enough of my taunts. What you really need to do is to learn about
reference-types and value-types and how classes and structs allocates on
heap and stack. And how they don't.

Jon Skeet, who posted above, have some excellent guides on that. Better than
any book...

Happy Reading
- Michael S

"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news3.zwoll1.ov.home.nl...
;-)

I see the problem (/solution) now.

Thanks!
Arjen

"Michael S" <a@b.c> schreef in bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
Have a look at Olivers answer.

At work I have a so called 'bug cup'. It's just a cup. Or sometimes a
glass or a mug.
But whenever I make silly mistakes, like you did; I have to give the cup
a coin.

My cup is almost always full! =)

- Michael S
"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news6.zwoll1.ov.home.nl...
I don't see what your point is.
I start with "myPerson = new Person();", try to get it from the cache,
if there is no cache then it is null.
(With null I mean that the properties like name and id are not set.)

About the while, you are correct because there is one result.
(But I use the same code where I get back multiple records)

Arjen

"Michael S" <a@b.c> schreef in bericht
news:Op**************@TK2MSFTNGP12.phx.gbl...
I think you missed including this line after you know myPerson is
null...

myPerson = new Person();

And why you try reassign this null reference in a while-loop is beyond
my understanding... =)

Happy Coding
- Michael S

"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news3.zwoll1.ov.home.nl...
> 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!
>



Nov 17 '05 #19

"Arjen" <bo*****@hotmail.com> wrote in message
news:de**********@news1.zwoll1.ov.home.nl...
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?


Provide the failing code, or better yet, a simplified example that displays
the same problem.
Nov 17 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fabio Papa | last post by:
Hi, I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :) I am writing a...
7
by: David Elliott | last post by:
I have created an application that will dynamically load other DLLs (plugins). The new plugin is a winform with an embedded IE Browser. I am wanting to have the form to run in its own thread....
0
by: Arjen | last post by:
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...
5
by: AAguiar | last post by:
I have an asp.net project where the code behind the aspx page calls a c# class which makes calls to a managed static C++ class. The C# class works fine when the asp net worker process starts, when...
2
by: R. van der Welle | last post by:
Hi All, I am upgrading my app (working well with VB6) to VB.NET. It sends MIDI messages using the API multimedia winmm.dll . These functions are declared: Public Declare Function timeSetEvent...
3
by: roopsd | last post by:
I am using VS.NET 2003 with crystal reports XI release 2. I get a runtime error at the following line: oRptForm.crystalReportViewer.ReportSource = oReport An unhandled exception of type...
1
by: musosdev | last post by:
Hi peeps, I'm suddenly getting a very strange error on one of my websites. It seems to be crashing within SaveViewState (?) but I can't tell if that the case, or if there is something else going...
3
by: Sagar | last post by:
Hi. I am working on a project to migrate a web application from 1.1 to 2.0 Within in the DAL of the application, there is a call to below function that builds a command object for later use. ...
1
by: r035198x | last post by:
This exception occurs often enough in practice to warrant its own article. It is a very silly exception to get because it's one of the easiest exceptions to avoid in programming. Yet we've all got it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.