Solved

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

Posted on 2010-09-23
4
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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