1

I have the following code below. Which works great expect I want to allow MaxDemand to be a null value. But since i'm parsing a string it seems to error if I don't put in some value.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Whats the best solution to implement an error handling solution? I tried float.TryParse but can't get that to work.

Thank you for looking over my issue.

    protected void GridView3_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    float Cost = 0.0F; 
    float Consumption = 0.0F;
    int InvoiceID = 0;
    float MaxDemand = 0.0F;
    DateTime ServiceFrom = new DateTime();
    DateTime ServiceTo = new DateTime();

    foreach (string key in e.NewValues.Keys)
        switch (key)
        {
            case "TotalInvoice": Cost = float.Parse(e.NewValues[key].ToString()); 
                                break;
            case "EnergyInvoiceID": InvoiceID = int.Parse(e.NewValues[key].ToString()); 
                                break;
            case "Consumption": Consumption = float.Parse(e.NewValues[key].ToString()); 
                                break;
            case "ServiceFrom": ServiceFrom = DateTime.Parse(e.NewValues[key].ToString());
                                break;
            case "ServiceTo":   ServiceTo = DateTime.Parse(e.NewValues[key].ToString());
                                break;
            case "MaxDemand": MaxDemand = float.Parse(e.NewValues[key].ToString());
                                break;
        }

    UpdateInvoice(InvoiceID, Cost, Consumption, ServiceFrom, ServiceTo, MaxDemand);
    GridView3.EditIndex = -1;
    PopulateAccountHistory();
}
1
  • 5
    Your problem is probably that one of those e.NewValues[key] is returning null and the .ToString() call fails when called on a null value, but I'm guessing; if you can post the stacktrace of the exception it will probably be clear Commented Oct 18, 2012 at 20:11

3 Answers 3

1

The problem here isn't in the actual parsing of the float or double value. If that failed it would return a more specific exception. It appears that the problem is one of the returns of e.NewValues[key] is returning null and you are throwing on the call to ToString

It would be best to centralize the null checks at the start of the loop. It would also allow you to remove the repeated calls to e.NewValues[key].

 foreach (string key in e.NewValues.Keys) {
   var value = e.NewValues[key];
   if (value == null) { 
       continue;
   }

   var str = value.ToString();
   switch (key) {
     case "TotalInvoice": 
       Cost = float.Parse(str);
       break;

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

1 Comment

Thanks JaredPar for explaining my issue and showing me how to fix it. This really helped and fixed my problem! I owe you a beer!
1

How about using a nullable type and checking the e.NewValues[Key] is not null

float? MaxDemand;

and then

case "MaxDemand": 
{
   if(e.NewValues[key] != null)
   {
     MaxDemand = float.Parse(e.NewValues[key].ToString());
   }
   break;
}

Comments

0

From my NullableExtensions:

public static T? Parse<T>(object obj)
    where T : struct
{
    if (obj == null) return null;
    try
    {
        return (T)System.Convert.ChangeType(obj, typeof(T));
    }
    catch (FormatException) { return null; }
    catch (InvalidCastException) { return null; }
}

case "TotalInvoice": Cost = NullableExtensions.Parse<float>(e.NewValues[key]); break;

1 Comment

Don't you need a where T : struct in that method signature?

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.