StreamReader NullReferenceException

3
我正在制作一个函数,它将从StreamReader中获取行数,但不包括注释(以'//'开头的行)和换行符。
这是我的代码:
private int GetPatchCount(StreamReader reader)
    {
        int count = 0;

            while (reader.Peek() >= 0)
            {
                string line = reader.ReadLine();
                if (!String.IsNullOrEmpty(line))
                {
                    if ((line.Length > 1) && (!line.StartsWith("//")))
                    {
                        count++;
                    }
                }
            }

        return count;
    }

我的StreamReader的数据是:

// Test comment

但是我遇到了一个错误,提示“对象引用未设置为对象实例”。有没有办法修复这个错误?

编辑 后来发现问题出在我的StreamReader为空。所以根据musefan和Mr. Smith建议的代码,我想到了以下解决方案:

private int GetPatchCount(StreamReader reader, int CurrentVersion)
    {
        int count = 0;
            if (reader != null)
            {
            string line;
            while ((line = reader.ReadLine()) != null)
                if (!String.IsNullOrEmpty(line) && !line.StartsWith("//"))
                    count++;
            }
        return count;
    }

感谢您的帮助!

堆栈跟踪是什么? - SLaks
你调试过这个吗?你可以轻松地设置断点并确定哪些内容是“null”。 - Arran
2
我猜你的StreamReader是空的... - MUG4N
@MUG4N:这不是必需的,会导致问题的是"\\"...算了 ;) - musefan
4个回答

2

不需要使用Peek(),这可能也是问题所在。你可以这样做:

string line = reader.ReadLine();
while (line != null)
{
    if (!String.IsNullOrEmpty(line) && !line.StartsWith("//"))
    {
        count++;
    }
    line = reader.ReadLine();
}

当然,如果你的StreamReader为null,则存在问题,但仅凭你的示例代码是不足以确定这一点的 - 你需要进行调试。应该有足够的调试信息让你找出哪个对象实际上为null。

哦,你说得对。这是因为我的StreamReader为空。谢谢! - CudoX

1
稍微整洁一些的代码,来自musefan的建议,只有一个ReadLine()代码。顺便赞同建议删除Length检查的+1。
private int GetPatchCount(StreamReader reader)
{
    int count = 0;
    string line;
    while ((line = reader.ReadLine()) != null)
        if (!String.IsNullOrEmpty(line) && !line.StartsWith("//"))
            count++;
    return count;
}

1
听起来你的reader对象是null
你可以通过以下方式检查读取器是否为null:
if (reader == null) {
   reader = new StreamReader("C:\\FilePath\\File.txt");
} 

@mattytommo 老板不在 ;) - Darren

0

你的代码信息不足以解决你的问题。我基于我的猜测用VS 2010制作了一个小应用程序,它可以正常工作。我相信你的代码在streamReader方面出现了问题。如果streamReader为空,你的代码将会抛出“对象引用未设置为对象的实例”。 你应该检查streamReader不为空,并确保streamReader可用。

您可以参考以下代码。请确保TextFile1.txt存在于D:\上。希望能对您有所帮助。

namespace ConsoleApplication1
{
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        using (StreamReader streamReader = new StreamReader(@"D:\\TextFile1.txt"))
        {
            int count = GetPatchCount(streamReader);
            Console.WriteLine("NUmber of // : {0}",  count);
        }

        Console.ReadLine();
    }

    private static int GetPatchCount(StreamReader reader)
    {
        int count = 0;

        while (reader.Peek() >= 0)
        {
            string line = reader.ReadLine();
            if (!String.IsNullOrEmpty(line))
            {
                if ((line.Length > 1) && (!line.StartsWith("//")))
                {
                    count++;
                }
            }
        }

        return count;
    }
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接