Solved

Reflection GetValue method throwing an exception when there is no value in the property?

Posted on 2010-09-23
4
366 Views
Last Modified: 2013-11-07
hi,

I am using Reflection to check if a property exists and get the value.
It is working fine when there is a value in that property. But "GetValue" is throwing an exception "Object reference not set to instance of an object" when there is no value.

How can I make sure it does not throw exception if there is no value. It looks like I need to check some other property before invoking GetValue. Thanks for any help.

PropertyInfo info = null;
info = Parties[i - 1].GetType().GetProperty(partyfield.Node);
if (info != null)
{
fieldvalue = info.GetValue(Parties[i - 1], null).ToString();
}
0
Comment
Question by:ipjyo
  • 2
4 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 250 total points
ID: 33748854
It's your use of ToString() which is causing the error. What is the Type of the property you are retrieving?

Wayne
0
 
LVL 16

Assisted Solution

by:kris_per
kris_per earned 250 total points
ID: 33749201

webtubbs has correctly pointed out the reason...to prevent exception, your code should be like:



PropertyInfo info = null;

info = Parties[i - 1].GetType().GetProperty(partyfield.Node);

if (info != null)

{

      object objValue = info.GetValue(Parties[i - 1], null);

      if(objValue != null) fieldvalue = objValue.ToString(); // depending on type of objvalue, ToString will return value of type name; you have to make sure you get value

      else fieldvalue = "";

} 

Open in new window

0
 

Author Comment

by:ipjyo
ID: 33753649
Thank you.
0
 

Author Closing Comment

by:ipjyo
ID: 33753658
It is working now.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now