Тип "Склад": "Ссылка на объект не указывает на экземпляр объекта" - C#
Формулировка задачи:
#region using
using System;
using System.IO;
#endregion
namespace ConsoleApplicationTest
{
public static class Program
{
private static void Main ( )
{
int key = 300; //собираюсь записывать в файл то, что ниже key по количеству на складе
string[] allLines = File.ReadAllLines("input.txt");
// массив Depots
Depot[] depots = new Depot [allLines.Length];
int count = 0;
for (int index = 0; index < allLines.Length; index++)
{
string line = allLines[index];
string[] fields = line.Split(';');
Depot depot = new Depot(fields[0], Convert.ToInt32(fields[1]), fields[2], Convert.ToInt32(fields[3]));
int tmp = Convert.ToInt32(fields[3]);
depots[index] = depot;
for (int i = 0; i < depots.Length; i++)
{
if (tmp < key)
{
count++;
}
}
string [] linesToSave = new string[count]; //массив для записи
//for (int i = 0; i < linesToSave.Length; i++)
//{
// if (tmp < key)
// {
// linesToSave[i] = depots[i].ToString();
// }
//}
// Сохраняем в файл
File.WriteAllLines("output.txt", linesToSave);
}
// Выводим данные
foreach (Depot depot in depots)
{
Console.WriteLine(depot);
Console.WriteLine();
}
Console.WriteLine("АниКей, чтоб Эксит...");
Console.ReadLine();
}
}
public class Depot
{
public Depot (string type, int price, string sort, int quantity)
{
Type = type;
Price = price;
Sort = sort;
Quantity = quantity;
}
//Тип овощя
public string Type
{
get;
private set;
}
// цена овощя
public int Price
{
get;
private set;
}
// сорт овощя
public string Sort
{
get;
private set;
}
// колчиство
public int Quantity
{
get;
private set;
}
public int CompareTo (object obj)
{
return Quantity.CompareTo(((Depot)obj).Quantity);
}
public override string ToString ( )
{
return string.Format("{0}; {1}; {2}; {3}", Type, Price, Sort, Quantity);
}
}
}Решение задачи: «Тип "Склад": "Ссылка на объект не указывает на экземпляр объекта"»
textual
Листинг программы
if(!string.IsNullOrEmptry(line))