簡體   English   中英

解析文件但獲取NullReference異常?

[英]Parsing file but get a NullReference exception?

對不起標題,老實說,我不知道該如何描述問題。 無論如何,這是我的困境。 我目前正在解析一個信息文件。 它的“作者”和“依賴項”部分相似。 但是,信息文件的創建者可以選擇以其他方式編寫他們的信息。 可以這樣寫:

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

要么:

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

我需要將兩種樣式都轉換為:

  author1, author2 

我要解析的文件如下所示:

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

我已經成功地解析了作者編寫它的第二種方式。 但是,當我嘗試解析第一種方法時,我收到了NullReference異常。 異常詳細信息如下:

用戶代碼HResult = -2147467261未處理System.NullReferenceException,消息=對象引用未設置為對象的實例。 Source = RKs工具套件StackTrace:位於C:\\ Users \\ Sam \\ Desktop \\ Programing \\ C#Projects \\ RK Mod Installer \\ RK Mod Installer \\ RK Mod Installer \\ ParseModInfo.cs:line 119中的RK_Tool_kit.ParseModInfo.getAuthors(字符串路徑)在RK_Tool_kit.AddMods.txtbxAddMods_DragDrop(Object sender,DragEventArgs e)在C:\\ Users \\ Sam \\ Desktop \\ Programing \\ C#Projects \\ RK Mod Installer \\ RK Mod Installer \\ RK Mod Installer \\ AddMods.cs:System.Windows中的第85行System.Windows.Forms.Control.System.Windows.Forms.IDropTarget.OnDragDrop(DragEventArgs drgEvent)的System.Windows.Forms.DropTarget.System.Windows.Forms.UnsafeNativeMethods.IOleDropTarget的.Forms.Control.OnDragDrop(DragEventArgs drgevent) .OleDrop(對象pDataObj,Int32 grfKeyState,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";
    }

如果已到達流的末尾,則StreamReader.ReadLine返回null,因此您可能會得到一個空行,並嘗試在其上調用Replace("\\"", "")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM