NullReferenceException inside using statement

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

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!
 
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!
 
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
 
I'm leaving a few lines below in sequence while cutting out others.
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
 
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 said:
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 said:
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!
 
Arjen said:
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.
 
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 said:
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 said:
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 said:
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!
 
;-)

I see the problem (/solution) now.

Thanks!
Arjen



Michael S said:
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 said:
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 said:
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





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!
 
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 said:
;-)

I see the problem (/solution) now.

Thanks!
Arjen



Michael S said:
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 said:
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" <[email protected]> schreef in bericht
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





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!
 
Arjen said:
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.
 
Back
Top