Solved

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

Posted on 2010-09-23
4
368 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

713 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