7

I'm getting this:

    private object setReportValues(object report, FormCollection values)
    {
        PropertyInfo[] properties = report.GetType().GetProperties();
        foreach (PropertyInfo property in properties)
        {
            string val = values.GetValue(property.Name).ToString();
            property.SetValue(report, val, null);
        }
        return report;
    }

Exception is on string val = values.GetValue(property.Name).ToString();. Do I have to check for nulls before?

6
  • What line is the error thrown on? Commented Jul 23, 2012 at 11:48
  • What is values? What is report? Why are you using reflection? Commented Jul 23, 2012 at 11:51
  • 4
    Hi, and welcome to SO! In the future, it's advisable to also include the exception information, such as which line actually throws the exception. That way, we'll be better equipped to help you. =) Commented Jul 23, 2012 at 11:51
  • @J.Steen go for the summer of love :) Commented Jul 23, 2012 at 11:51
  • @the_ajp Hell yeah. I noticed myself getting bitter and elitist, so I wholly support the initiative. =) Commented Jul 23, 2012 at 11:52

4 Answers 4

5

Do I have to check for nulls before?

On this line, yes:

string val = values.GetValue(property.Name).ToString()

Simply because the value of that particular property could be null.

Sign up to request clarification or add additional context in comments.

Comments

3

I'm gonna go out on a limb and suggest that there's no property with the provided property.Name in values. So your call to values.GetValue returns a null. When you try to do ToString() on that null value, it complains.

In short, what does your values variable contain?

Update:

With the provided information that values is a FormsCollection it is quite probable that your properties collection contains a few properties for which you have no FormsCollection field. And what happens is that you try to get this field, it returns a null value and you call ToString on that, causing everything to break.

I would invert my strategy and loop through my FormsCollection getting the properties 1 by 1 as you encounter them. The alternative is to keep it as you have it and check for null before doing a ToString.

PS: I hope all of your properties represented on the form are strings, or things will break.

3 Comments

Getting them 1 by 1 is cumbersome. There are also dates on the form.
I meant 1 by 1 dynamically within the loop, which I don't really find cumbersome. But that's entirely up to you and taste I guess. At least now you know how it fits together. Happy coding! :)
Thank you, changing prop.GetValue(instance).ToString() to prop.GetValue(instance)?.ToString() ?? null did the trick at my place...
1

Just ran into this same issue, but I found a solution without having to use a loop:

private object setReportValues(object report, FormCollection values)
{
    PropertyInfo[] properties = report.GetType().GetProperties();
    foreach (PropertyInfo property in properties)
    {
        string val = values.GetValue(property.Name)?.ToString();
        property.SetValue(report, val, null);
    }
    return report;
}

I fixed it by adding a ? (new feature in C# 6.0 I believe) after the ...property.Name).

Comments

0

Why would you force .ToString() ? null is a perfectly legal value for most things. It isn't clear what values is, so I assume that is coming from your own code, but:

object val = values.GetValue(property.Name);
property.SetValue(report, val, null);

Depending on what values is, you might also want to check the difference between "has a value, that is null" vs "doesn't have any defined value". Personally, I would expect to do something like:

object val;
if(values.TryGetValue(property.Name, out val)) {
    property.SetValue(report, val, null);
}

1 Comment

Need strings for further actions:), thnx btw.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.