我想在我的项目中使用我在网上找到的CSV解析器。问题是当我尝试将字符串转换为标记并且我的集合没有被填充时,我得到空引用异常。任何人都可以协助由于C#NullReferenceException未处理
CSV分析器
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;
}
标记类
public class Tag
{
public Tag(string name, int weight)
{
Name = name;
Weight = weight;
}
public string Name { get; 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));
}
}
抛出异常在哪里?看看堆栈跟踪。 – 2011-03-18 10:22:48
查看有关异常的堆栈跟踪,它应该精确地指出代码中的哪一行引发了异常。你也可以调试你的代码,当它抛出异常时它会中断,你可以发现什么变量是null。如果你有一个异常处理程序,你需要进入Debug-> Exceptions对话框,并且标记Thrown复选框以查找抛出异常的异常。 – 2011-03-18 10:22:49
2个代码片段之间的连接似乎缺失。 – 2011-03-18 10:23:44