2013-03-24 97 views
0

对不起,我真的不知道如何描述那里的问题。无论如何,这是我的困境。我目前正在解析一个信息文件。它的“作者”和“依赖”部分是相似的。但是,info文件的创建者可以选择以不同的方式编写它们。可以这样写:解析文件但得到NullReference异常?

"authors" : 
[ 
    "author1", 
    "author2" 
], 

或:

"authors": ["author1", "author 2"], 

我需要把两者的风格分为:

author1, author2 

文件我试图解析如下所示:

-snip 
"authors": [ 
"author1", 
"author2", 
"author3" 
-snip 

我已经成功地解析作者写的第二种方式。但是,当我尝试解析第一种方式时,我得到一个NullReference异常。异常的详细信息变为如下:

System.NullReferenceException是由用户代码的HResult = -2147467261消息=对象引用不设置为一个对象的一个​​实例 未处理。 Source = RKs Tool Kit StackTrace: at C:\ Users \ Sam \ Desktop \ Programing \ C#Projects \ RK Mod Installer \ RK中的RK_Tool_kit.ParseModInfo.getAuthors(String path)Mod Installer \ RK Mod Installer \ ParseModInfo.cs:第一行119 位于C:\ Users \ Sam \ Desktop \ Programing \ C#Projects \ RK中的RK_Tool_kit.AddMods.txtbxAddMods_DragDrop(Object sender,DragEventArgs e) 在System.Windows.Forms.Control.OnDragDrop(DragEventArgs drgevent) 在System.Windows.Forms.Control.System.Windows.Forms.IDropTarget.OnDragDrop(DragEventArgs drgEvent) 在System.Windows.Forms.DropTarget.System .Windows.Forms.UnsafeNativeMethods.IOleDropTarget.OleDrop(Object pDataObj,Int32 grfKeySt吃,POINTSTRUCT PT,的Int32 & pdwEffect)
的InnerException:

我在这里的损失。非常感谢提前。这里是我的代码:

public static string getAuthors(string path) 
    { 
     string line; 

     using (StreamReader sr = new StreamReader(path)) 
     { 
      while (!sr.EndOfStream) 
      { 
       line = sr.ReadLine(); 
       if (line.Contains("author")) 
       { 
        //This detects if it is the second way the authors can write it 
        if (line.Contains("[") && line.Contains("]")) 
        { 
         line = line.Replace("\"authors\": ", "").Replace("\"authors\" : ", "").Replace("\"authorList\": ", "").Replace("\"authorList\" : ", "").Replace("[", "").Replace("]", "").Replace("\"", "").TrimStart(toTrim); 
         int Place = line.LastIndexOf(","); 
         string comma = ","; 
         line = line.Remove(Place, comma.Length).Insert(Place, ""); 
         return line; 
        } 
        //This means the the author wrote it the first way. And the string "line" just says '"authors": [' so we want to read the line again. 
        else 
        { 
         line = sr.ReadLine(); 
         line = line.Replace("\"", "").TrimStart(toTrim); 

         for (int i = 0; i < 101; i++) 
         { 
          //This checks to see if there is only one author in the array. If there is a comma, we want to read another line because there is another author. 
          if (line.Contains(",")) 
          { 
           //THIS RIGHT HERE throws the exception 
           line = line + sr.ReadLine().Replace("\"", "").TrimStart(toTrim); 
          } 
          else 
          { 
           break; 
          } 
         } 

         return line; 
        } 
       } 
      } 
     } 

     return "N/A"; 
    } 
+0

NullReferenceException发生在哪一行? – 2013-03-24 00:13:39

+0

它在代码中作为评论, – 2013-03-24 00:13:58

+0

@TheGreatCO迈克C.说什么。 – user1522456 2013-03-24 00:17:16

回答

3

StreamReader.ReadLine,则返回null流的末尾已到达,所以你很可能得到一个空行,并试图调用Replace("\"", "")就可以了。

+0

我原本也认为,但事实并非如此。我知道接下来会有一些事情。而且,如果流的结束已经达到,它会刚刚返回“N/A”。 – user1522456 2013-03-24 00:19:24

+0

@ user1522456 - 您正在'while'循环的开始处检查流的结束,但是然后从阅读器读取多行而不检查空值。 'ReadLine'是该行唯一可以返回null的方法。尝试将其读入临时字符串,并在引发异常时附加调试器,以便检查它。 – Lee 2013-03-24 00:23:36

+0

啊,这很有道理。我会测试一下。 – user1522456 2013-03-24 00:30:20