How to handle this exception?

G

Guest

Hello, friends,

In the following C# code, I tried to get the values for each property in an
object:

public void WriteObjectValue(object obj)
{
Type objectType = obj.GetType();
PropertyInfo[] properties = objectType.GetProperties();

foreach (PropertyInfo property in properties)
{
System.Object propertyValue = property.GetValue(obj, null);
Console.WriteLine(propertyValue.ToString());
}
}

This worked fine, except in the following situation:

If the passed object obj was defined like the follows

Public class IntObjClass
{
int? deptId;
int? sectionId;

public int DeptId {set {deptId = value;}}
public int SectioinId {set {sectionId = value;}}

}

and only sectionId was assigned a value, say 100, while deptId was left null.

At this situation, the foreach loop will throw an exception at
System.Object propertyValue = property.GetValue(),
complaining deptId was null.

Is there a way to get around this? (I don't want to use try{} catch {} to
handle this exception)

Thanks a lot for your help.
 
H

Henning Krause [MVP - Exchange]

Hello,

Have you tried this?

if (propertyValue == null) {
Console.WriteLine("<null>");
}
else
{
Console.WriteLine(propertyValue.ToString());
}

Best regards,
Henning Krause
 
G

Guest

It was the

propertyValue = property.GetValue(obj, null);

throw the exception (Nullable object must have a value) before I had a
chance to check if properValue was null or not.

Any other ideas? Thanks.

Henning Krause said:
Hello,

Have you tried this?

if (propertyValue == null) {
Console.WriteLine("<null>");
}
else
{
Console.WriteLine(propertyValue.ToString());
}

Best regards,
Henning Krause

Andrew said:
Hello, friends,

In the following C# code, I tried to get the values for each property in
an
object:

public void WriteObjectValue(object obj)
{
Type objectType = obj.GetType();
PropertyInfo[] properties = objectType.GetProperties();

foreach (PropertyInfo property in properties)
{
System.Object propertyValue = property.GetValue(obj,
null);
Console.WriteLine(propertyValue.ToString());
}
}

This worked fine, except in the following situation:

If the passed object obj was defined like the follows

Public class IntObjClass
{
int? deptId;
int? sectionId;

public int DeptId {set {deptId = value;}}
public int SectioinId {set {sectionId = value;}}

}

and only sectionId was assigned a value, say 100, while deptId was left
null.

At this situation, the foreach loop will throw an exception at
System.Object propertyValue = property.GetValue(),
complaining deptId was null.

Is there a way to get around this? (I don't want to use try{} catch {} to
handle this exception)

Thanks a lot for your help.
 
V

VJ

more like

if (property == null) {

}
else
{

}

should work

VJ

Andrew said:
It was the

propertyValue = property.GetValue(obj, null);

throw the exception (Nullable object must have a value) before I had a
chance to check if properValue was null or not.

Any other ideas? Thanks.

Henning Krause said:
Hello,

Have you tried this?

if (propertyValue == null) {
Console.WriteLine("<null>");
}
else
{
Console.WriteLine(propertyValue.ToString());
}

Best regards,
Henning Krause

Andrew said:
Hello, friends,

In the following C# code, I tried to get the values for each property
in
an
object:

public void WriteObjectValue(object obj)
{
Type objectType = obj.GetType();
PropertyInfo[] properties = objectType.GetProperties();

foreach (PropertyInfo property in properties)
{
System.Object propertyValue = property.GetValue(obj,
null);
Console.WriteLine(propertyValue.ToString());
}
}

This worked fine, except in the following situation:

If the passed object obj was defined like the follows

Public class IntObjClass
{
int? deptId;
int? sectionId;

public int DeptId {set {deptId = value;}}
public int SectioinId {set {sectionId = value;}}

}

and only sectionId was assigned a value, say 100, while deptId was left
null.

At this situation, the foreach loop will throw an exception at
System.Object propertyValue = property.GetValue(),
complaining deptId was null.

Is there a way to get around this? (I don't want to use try{} catch {}
to
handle this exception)

Thanks a lot for your help.
 
G

Guest

property is not null. It is a valid instantiated object.

VJ said:
more like

if (property == null) {

}
else
{

}

should work

VJ

Andrew said:
It was the

propertyValue = property.GetValue(obj, null);

throw the exception (Nullable object must have a value) before I had a
chance to check if properValue was null or not.

Any other ideas? Thanks.

Henning Krause said:
Hello,

Have you tried this?

if (propertyValue == null) {
Console.WriteLine("<null>");
}
else
{
Console.WriteLine(propertyValue.ToString());
}

Best regards,
Henning Krause

Hello, friends,

In the following C# code, I tried to get the values for each property
in
an
object:

public void WriteObjectValue(object obj)
{
Type objectType = obj.GetType();
PropertyInfo[] properties = objectType.GetProperties();

foreach (PropertyInfo property in properties)
{
System.Object propertyValue = property.GetValue(obj,
null);
Console.WriteLine(propertyValue.ToString());
}
}

This worked fine, except in the following situation:

If the passed object obj was defined like the follows

Public class IntObjClass
{
int? deptId;
int? sectionId;

public int DeptId {set {deptId = value;}}
public int SectioinId {set {sectionId = value;}}

}

and only sectionId was assigned a value, say 100, while deptId was left
null.

At this situation, the foreach loop will throw an exception at
System.Object propertyValue = property.GetValue(),
complaining deptId was null.

Is there a way to get around this? (I don't want to use try{} catch {}
to
handle this exception)

Thanks a lot for your help.
 
V

VJ

Ok Andrew, I think I know the problem.. sorry about confusion. You cannot do
getValue on a property with Set only attributes. It wont work. How you will
know a property is read or write is like below

property.CanRead
or
property.CanWrite

HTH
VJ

Andrew said:
property is not null. It is a valid instantiated object.

VJ said:
more like

if (property == null) {

}
else
{

}

should work

VJ

Andrew said:
It was the

propertyValue = property.GetValue(obj, null);

throw the exception (Nullable object must have a value) before I had a
chance to check if properValue was null or not.

Any other ideas? Thanks.

:

Hello,

Have you tried this?

if (propertyValue == null) {
Console.WriteLine("<null>");
}
else
{
Console.WriteLine(propertyValue.ToString());
}

Best regards,
Henning Krause

Hello, friends,

In the following C# code, I tried to get the values for each
property
in
an
object:

public void WriteObjectValue(object obj)
{
Type objectType = obj.GetType();
PropertyInfo[] properties = objectType.GetProperties();

foreach (PropertyInfo property in properties)
{
System.Object propertyValue = property.GetValue(obj,
null);
Console.WriteLine(propertyValue.ToString());
}
}

This worked fine, except in the following situation:

If the passed object obj was defined like the follows

Public class IntObjClass
{
int? deptId;
int? sectionId;

public int DeptId {set {deptId = value;}}
public int SectioinId {set {sectionId = value;}}

}

and only sectionId was assigned a value, say 100, while deptId was
left
null.

At this situation, the foreach loop will throw an exception at
System.Object propertyValue = property.GetValue(),
complaining deptId was null.

Is there a way to get around this? (I don't want to use try{} catch
{}
to
handle this exception)

Thanks a lot for your help.
 
G

Guest

sorry, it actually had {get {return ****;}} definitions....

so, ReadOnly was not the reason

VJ said:
Ok Andrew, I think I know the problem.. sorry about confusion. You cannot do
getValue on a property with Set only attributes. It wont work. How you will
know a property is read or write is like below

property.CanRead
or
property.CanWrite

HTH
VJ

Andrew said:
property is not null. It is a valid instantiated object.

VJ said:
more like

if (property == null) {

}
else
{

}

should work

VJ

It was the

propertyValue = property.GetValue(obj, null);

throw the exception (Nullable object must have a value) before I had a
chance to check if properValue was null or not.

Any other ideas? Thanks.

:

Hello,

Have you tried this?

if (propertyValue == null) {
Console.WriteLine("<null>");
}
else
{
Console.WriteLine(propertyValue.ToString());
}

Best regards,
Henning Krause

Hello, friends,

In the following C# code, I tried to get the values for each
property
in
an
object:

public void WriteObjectValue(object obj)
{
Type objectType = obj.GetType();
PropertyInfo[] properties = objectType.GetProperties();

foreach (PropertyInfo property in properties)
{
System.Object propertyValue = property.GetValue(obj,
null);
Console.WriteLine(propertyValue.ToString());
}
}

This worked fine, except in the following situation:

If the passed object obj was defined like the follows

Public class IntObjClass
{
int? deptId;
int? sectionId;

public int DeptId {set {deptId = value;}}
public int SectioinId {set {sectionId = value;}}

}

and only sectionId was assigned a value, say 100, while deptId was
left
null.

At this situation, the foreach loop will throw an exception at
System.Object propertyValue = property.GetValue(),
complaining deptId was null.

Is there a way to get around this? (I don't want to use try{} catch
{}
to
handle this exception)

Thanks a lot for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top