我收到一個錯誤,說我有一個數組超出索引,但我不知道我在做什麼錯誤。我已經注意到Visual Studio突出顯示的錯誤。它發生在ReadData()
方法中。感謝您的任何建議。嘗試從文本文件中讀取時數組超出索引,int32.tryparse
類型「System.IndexOutOfRangeException」的第一次機會異常發生在Lab3.exe 類型「System.IndexOutOfRangeException」的未處理的異常發生在Lab3.exe 其他信息:索引陣列的邊界之外。 程序'[9140] Lab3.vshost.exe:Managed(v4.0.30319)'已退出,代碼爲0(0x0)。
class Program
{
private const int MAX_MEDIA_OBJECTS = 100; // Max number of array objects
private int mediaCount = 0; // Counter to keep track of amount of media in Data.txt
private Media[] media = new Media[MAX_MEDIA_OBJECTS];
private Song[] songs = new Song[MAX_MEDIA_OBJECTS];
private Movie[] movies = new Movie[MAX_MEDIA_OBJECTS];
private Book[] books = new Book[MAX_MEDIA_OBJECTS];
static void Main(string[] args)
{
Program lab3 = new Program();
bool didUserExit = false;
int userSelectedOption;
lab3.ReadData();
do // Do While loop for the options menu, exits when the user selects the exit option.
{
lab3.DisplayOptions();
string userInput = Console.ReadLine();
if (int.TryParse(userInput, out userSelectedOption))
{
lab3.ProcessSelectedInput(userSelectedOption, lab3);
}
else
{
lab3.DisplayErrorMessage();
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
} while (!didUserExit);
}
public void ReadData()
{
// Opens the Data.txt file for read access
FileStream mediaFile = new FileStream("Data.txt", FileMode.Open, FileAccess.Read);
StreamReader mediaData = new StreamReader(mediaFile);
string mediaRow; // Holds each media data per row
while ((mediaRow = mediaData.ReadLine()) != null)
{
// Splits each row with the delimiter
string[] mediaDataSplit = mediaRow.Split('|');
// Temporary variables to hold media data
int year;
/** ERROR HAPPENS HERE ACCORDING TO VISUAL STUDIO IDE */
bool didConvert = Int32.TryParse(mediaDataSplit[2].Trim(), out year);
if (!didConvert)
{
Console.WriteLine("Improperly formated field at line {0}", mediaCount + 1);
Environment.Exit(0);
}
if (didConvert)
{
Console.WriteLine("trace year {0}", mediaDataSplit[2]);
}
mediaCount++;
}
}
public void ProcessSelectedInput(int userSelectedOption, Program labReference)
{
switch (userSelectedOption)
{
case 1:
Console.WriteLine("case1");
break;
case 2:
Console.WriteLine("case2");
break;
case 3:
Console.WriteLine("case3");
break;
case 4:
Console.WriteLine("case4");
break;
case 5:
Console.WriteLine("case5");
break;
case 6:
Environment.Exit(0);
break;
default:
labReference.DisplayErrorMessage();
break;
}
}
public void DisplayOptions()
{
Console.Clear();
Console.WriteLine("1. List All Books");
Console.WriteLine("2. List All Movies");
Console.WriteLine("3. List All Songs");
Console.WriteLine("4. List All Media");
Console.WriteLine("5. Search All Media by Title");
Console.WriteLine("");
Console.WriteLine("6. Exit Program");
Console.WriteLine("");
Console.Write("Enter choice: ");
}
public void DisplayErrorMessage()
{
Console.WriteLine("*** Invalid Choice - Try Again ***");
}
}
http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – SLaks
的問題不是特定的,請詳細說明錯誤發生的位置,至少包括一個堆棧跟蹤 –
您應該檢查SLak發佈的鏈接。如果你不能調試一個小而簡單的程序,那麼你的問題比數組超出界限要嚴重得多。 –