Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 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

13 Experts available now in Live!

Get 1:1 Help Now