0

请帮助我理解,这段代码有什么问题。(我正在尝试构建一个字符串,从文本文件中逐行获取其中的一部分)。

我在行上收到运行时错误“在对象引用的实例中未设置为对象”strbuild.Append(str);

        StreamReader reader = new StreamReader("buf.txt", System.Text.Encoding.ASCII);
        StringBuilder strbuild = new StringBuilder();
        strbuild = null;

        while (reader.Peek() >= 0)
        {
            string str = null;
            str = reader.ReadLine().ToString();

            string segment = str.Substring(0, 1);

            if (segment == "A")
            {
                strbuild.Append(str); //here  i get an error
            }
            else if (segment == "B")
            {
                strbuild.Append("BET");
            }

        }
        printstr = strbuild.ToString();
        reader.Close();

        MessageBox.Show(printstr);
4

4 回答 4

10

看看这些行:

StringBuilder strbuild = new StringBuilder();
strbuild = null;

当你打电话时你期望会发生什么strbuild.Append(...)?你为什么设置strbuild为null?

您似乎喜欢两行变量初始化 - 这是另一个示例:

string str = null;
str = reader.ReadLine().ToString();

这将更容易阅读:

string str = reader.ReadLine();

ReadLine已经返回一个字符串,所以你不需要调用ToString()结果。)

但是,我确实建议您using对 - 使用语句,StreamReader否则当抛出异常时,您将使阅读器保持打开状态。

一件好事TextReader.ReadLine()是当你完成时它返回 null 。你不需要偷看然后阅读。

最后,如果您只测试单个字符,则不需要子字符串 - 只需使用字符串索引器获取 char。所以,你可以有:

StringBuilder builder = new StringBuilder();

// Consider using File.OpenText
using (StreamReader reader = new StreamReader("buf.txt", Encoding.ASCII))
{
    string line;
    // Normally side-effect + test is ugly, but this is a common and
    // neat idiom
    while ((line = reader.ReadLine()) != null)
    {
        // TODO: What do you want to happen for empty lines?
        char segment = str[0];
        if (segment == 'A')
        {
            builder.Append(line);
        }
        else if (segment == 'B')
        {
            builder.Append("BET");
        }
    }
}
MessageBox.Show(builder.ToString());
于 2010-03-02T14:55:13.313 回答
6

您在初始化后将 stringbuilder 设置为 null 。

改变

StringBuilder strbuild = new StringBuilder(); 
strbuild = null; 

StringBuilder strbuild = new StringBuilder(); 

省去这条线

strbuild = null;
于 2010-03-02T14:55:05.547 回答
2

改变

  StringBuilder strbuild = new StringBuilder();
  strbuild = null;

  StringBuilder strbuild = null;
  strbuild = new StringBuilder();

或者,为了防止这种错误:

  StringBuilder strbuild = new StringBuilder();
于 2010-03-02T14:55:56.933 回答
1

您的示例中有很多错误,这是第一个更正的版本:

StringBuilder strbuild = new StringBuilder();

// Put resource into using statements, for deterministic cleanup
using (TextReader reader = new StreamReader("buf.txt", System.Text.Encoding.ASCII))
{
    string line;

    //Maybe looks a little ugly the first time, but is commonly used to
    //process all the lines of a file (.ReadToEnd() can cause memory problems
    //on really big files)
    while ((line = reader.ReadLine()) != null)
    {
        //Instead of if, else if, else if, etc just take a switch
        //statement. Makes it much easier to read.
        switch (line[0])
        {
            //If you need case insensitivity put both versions of the character
            //before your first line of code
            case 'A':
            case 'a':
                strbuild.Append(line);
                break;
            //Otherwise just use the lower or upper case version you like
            case 'B':
                strbuild.Append("BET");
                break;
        }
    }
}

//Put the result of the StringBuilder directly into the Show() function
MessageBox.Show(strbuild.ToString());
于 2010-03-02T15:06:55.290 回答