对不起,我真的不知道如何描述那里的问题。无论如何,这是我的困境。我目前正在解析一个信息文件。它的“作者”和“依赖”部分是相似的。但是,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";
}
NullReferenceException发生在哪一行? – 2013-03-24 00:13:39
它在代码中作为评论, – 2013-03-24 00:13:58
@TheGreatCO迈克C.说什么。 – user1522456 2013-03-24 00:17:16