Solved

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

Posted on 2010-09-23
4
365 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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

705 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

16 Experts available now in Live!

Get 1:1 Help Now