开发者

C# NullReferenceException was unhandled

I am trying to use a CSV parser which I found on the net in my project. The problem is I am getting a null reference exception when I try to convert the string to a Tag and my collection does not get populated. Can anyone assist? Thanks

CSV Parser

private static IEnumerable<string[]> parseCSV(string path)
    {
        List<string[]> parsedData = new List<string[]>();

        try
        {
            using (StreamReader readFile = new StreamReader(path))
            {
                string line;
                string[] row;

                while ((line = readFile.ReadLine()) != null)
                {
                    row = line.Split(',');
                    parsedData.Add(row);
                }
            }
        }
        catch (Exception e)
        {
            System.Windows.MessageBox.Show(e.Message);
        }

        return parsedData;
    }

Tag Class

  public class Tag
    {
        public Tag(string name, int weight)
        {
            Name = name;
            Weight = weight;
        }

        public string Name { ge开发者_开发百科t; set; }
        public int Weight { get; set; }

        public static IEnumerable<Tag> CreateTags(IEnumerable<string> words)
        {
            Dictionary<string, int> tags = new Dictionary<string, int>();

            foreach (string word in words)
            {
                int count = 1;
                if (tags.ContainsKey(word))
                {
                    count = tags[word] + 1;
                }

                tags[word] = count;
            }

            return tags.Select(kvp => new Tag(kvp.Key, kvp.Value));
        }
    }


Validate all method arguments before you use them!

It breaks on this line: foreach (string word in words)

Remember that foreach loops work by calling GetEnumerator on the collection iterated over. That is, your foreach loop causes a call to words.GetEnumerator, and this call fails if words is null.

Therefore, validate your argument words by adding a guard at the very start of your CreateTags method:

if (words == null)
{
    throw new ArgumentNullException("words");
}

This will help you find the location in your code where null is passed into CreateTags, and you can then continue fixing the calling code.

Suggestion: Avoid null whenever possible.

As a very general rule, try to avoid using null values whenever possible. For example, when your code is dealing with sets and collections of items, you could make sure that it also works correctly with empty collections. In a second step, make sure that you never use null to represent an empty collection; instead, use e.g. LINQ's Enumerable.Empty<TItem>() generator to create an empty collection.

One place where you could start doing this is in the CreateTags method by ensuring that no matter what the inputs are, that method will always return a valid, non-null (but possibly empty) collection:

if (words == null)
{
    return Enumerable.Empty<Tag>();  // You could do without LINQ by writing:
                                     // return new Tag[] { };
}


Every method should run sanity checks on the arguments it accepts to ensure the arguments are valid input parameters. I would probably do something like

public static IEnumerable<Tag> CreateTags(IEnumerable<string> words)
    {
        if(words==null)
        {
           //either throw a new ArgumentException or
           return null; //or return new Dictionary<string,int>();
        }
        Dictionary<string, int> tags = new Dictionary<string, int>();

        foreach (string word in words)
        {
            int count = 1;
            if (tags.ContainsKey(word))
            {
                count = tags[word] + 1;
            }

            tags[word] = count;
        }

        return tags.Select(kvp => new Tag(kvp.Key, kvp.Value));
    }

As to why your "words" param is null, it would be helpful to see the CSV file you are trying to parse in.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜