我需要从文本文件中读取,然后将每一行放入一个列表中,然后从该列表中读取。但我得到一个 NullReferenceException “对象引用未设置为对象的实例。” 在大约 7 行的同时异常。我已经尝试了我能想到的一切。提前致谢。
StreamReader sre = new StreamReader(FILE_PATH);
Books books = new Books();
string line;
while ((line = sre.ReadToEnd()) != null)
{
//NullReferenceException is Right here
//I defined myLibraryBooks outside of this code; But it is in the same scope
myLibraryBooks.Add(new Books() { Author = books.Author.ToUpper(), Title = line.ToUpper(), ISBN = line, Publish_Date = line });
}
Console.Write("Enter Author's Name:");
string input_to_find = Console.ReadLine();
var author = from Authors in myLibraryBooks
where Authors.Author == input_to_find
select Authors;
foreach (var book in author)
{
Console.WriteLine(String.Format(" Author Title ISBN Publish Date"));
Console.WriteLine(String.Format(" {0} {1} {2} {3}", books.Author, books.Title, books.ISBN, books.Publish_Date));
}
sre.Dispose();