2010-03-02 66 views
0

请帮我理解,这段代码有什么问题。 (我试图建立一个字符串,从文本文件中逐行读取它的一部分)。StringBuilder实例中的运行时错误

我得到一个运行时错误“在不设置到对象的对象引用的实例”上线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); 

回答

10

看看这些行:

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

你是什么预计发生时,然后你打电话strbuild.Append(...)?为什么你将strbuild设置为空?

你似乎喜欢两行变量初始化 - 这里是另一个例子:

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

这将是更容易阅读的只是:

string str = reader.ReadLine(); 

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

但是,我建议您为StreamReader使用using声明 - 否则,当抛出异常时,您将打开阅读器。

TextReader.ReadLine()的一个好处是,它完成后返回null。你不需要偷看,然后然后阅读。

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

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()); 
+0

非常感谢您的好评! – rem 2010-03-03 08:27:04

6

您初始化后的StringBuilder设置为NULL。

变化

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

StringBuilder strbuild = new StringBuilder(); 

离开了行

strbuild = null; 
2

变化

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

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

,或者防止这种错误:

StringBuilder strbuild = new StringBuilder(); 
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()); 
+0

虽然很高兴为您清理它,但您没有解释为什么您首先进行了更改或导致问题的原因。 – Joshua 2010-03-02 15:10:06

+0

@Joshua:所以给代码添加了一些评论。希望这有助于;-) – Oliver 2010-03-02 15:21:14

+0

更好的答案! :) – Joshua 2010-03-02 16:52:22