2010-03-02 148 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