0

I wasn't the one who coded this line of code, and I can't gt to understand whoever did it, why did this way on this line: res.Data.Find(itm => itm.Meta.ID.ToUpper() == i["MetaDataID"].ToString().ToUpper()).Value[i["LocaleID"].ToString()] = i["Value"].ToString(); And this line gives me a NullReferenceException. How can I get around it?

public static void LoadData(Value.Item _res)
    {
        DataTable Res = Connector.Run("SELECT * FROM Data WHERE ItemID='" + _res.ID + "'");

        if (Res.Rows.Count != 0)
        {
            foreach (DataRow i in Res.Rows)
            {
                try
                {
                    _res.Data.Find(itm => itm.Meta.ID.ToUpper() == i["MetaDataID"].ToString().ToUpper()).Value[i["LocaleID"].ToString()] = i["Value"].ToString();
                }
                catch (Exception)
                {
                    _res.Data.Add(new Value.Data(
                                      i["ID"].ToString(),
                                      i["Value"].ToString(),
                                      i["LocaleID"].ToString(),
                                      i["MetaDataID"].ToString()
                                      ));
                }
            }
        }
    }

Thx a lot guys!!! Here is my working solution which hrows no Exceptions anymore!

 public static void LoadData(Value.Item _res)
    {
        DataTable Res = Connector.Run("SELECT * FROM Data WHERE ItemID='" + _res.ID + "'");

        if (Res.Rows.Count != 0)
        {
            foreach (DataRow i in Res.Rows)
            {
                bool _flagged = false;

                var _result = _res.Data.Find(itm => itm.Meta.ID.ToUpper() == i["MetaDataID"].ToString().ToUpper());

                if(_result != null && i["LocaleID"] != null)
                {
                    if (i["Value"] == null || i["LocaleID"] == null || i["MetaDataID"] == null)
                        _flagged = true;

                }
                else
                {
                    _flagged = true;
                }


                if (_flagged)
                {
                    _res.Data.Add(new Value.Data(
                                      i["ID"].ToString(),
                                      i["Value"].ToString(),
                                      i["LocaleID"].ToString(),
                                      i["MetaDataID"].ToString()
                                      ));
                }

                //try
                //{
                //    _res.Data.Find(itm => itm.Meta.ID.ToUpper() == i["MetaDataID"].ToString().ToUpper()).Value[i["LocaleID"].ToString()] = i["Value"].ToString();
                //}
                //catch (Exception)
                //{
                //    _res.Data.Add(new Value.Data(
                //                      i["ID"].ToString(),
                //                      i["Value"].ToString(),
                //                      i["LocaleID"].ToString(),
                //                      i["MetaDataID"].ToString()
                //                      ));
                //}
            }
        }
    }
2
  • By the way, you do know that it is more efficient to use string.Compare() rather than ToUpper() on both sides of a string? Commented Nov 24, 2011 at 16:42
  • hm i didn't know it was... I will replace. Gotta do this on other codes too but thkx a lot for the tip!!! Commented Nov 24, 2011 at 18:36

3 Answers 3

2

Impossible to answer accurately without having the code under the debugger, however what is certain is that at least one of the following has a value of null:

_res.Data
itm.Meta
itm.Meta.ID
i["MetaDataID"]
i["LocaleID"]
i["Value"]
Sign up to request clarification or add additional context in comments.

Comments

2

Old-skool debugging:

Break the statement down to it's consituent parts - using local variables. Step-through and find which one is null.

Comments

1

Check if one of the following are null:

i["MetaDataID"]
i["LocaleID"]
i["Value"]

Comments

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.