NullReferenceException-как да го избегна.
В една задача от Tech-модула (Immune System) компилатора ми върна NкullReferenceException.
Направих справка в интернет и в общи линии разбрах за какво става въпрос,но не мога да рзбера от къде се появява при мен.
Ето условието на задачата:
https://softuni.bg/trainings/resources/officedocument/31796/more-exercise-problem-descriptions-optional-programming-fundamentals-may-2018/1939,
а ето и моето решение:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Imunne_Sysrem
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> datesvirusstrench = new Dictionary<string, int>();
Dictionary<string, int> datestime = new Dictionary<string, int>();
Dictionary<string, int> viruscount = new Dictionary<string, int>();
int inicialhealth = int.Parse(Console.ReadLine());
double remeininghealth = inicialhealth;
int virusstrench = 0;
string[] viruslist = new string[100];
int counter = 0;
while (true)
{
int timetodefeat = 0;
int timeminute = 0;
int timeseconds =0;
int sum = 0;
string virusname = Console.ReadLine();
viruslist[counter] = virusname;
for (int i = 0; i < virusname.Length; i++)
{
sum += virusname[i];
}
virusstrench = sum / 3;
if (virusname == "end")
{
for (int i = 0; i < counter; i++)
{
if (viruscount.ContainsKey(viruslist[i]))
{
viruscount[viruslist[i]] += 1;
}
else
{
viruscount.Add(viruslist[i], 1);
}
if ((viruscount[viruslist[i]] == 1))
{
Console.WriteLine($"Virus {viruslist[i]}: {datesvirusstrench[viruslist[i]]} => {datestime[viruslist[i]]} seconds");
Console.WriteLine($"{viruslist[i]} defeated in {secondstominute(datestime[viruslist[i]])}m {secondstoostseconds(datestime[viruslist[i]])}s.");
remeininghealth -= datestime[viruslist[i]];
Console.WriteLine($"Remeining health: {Math.Floor(remeininghealth)}");
}
else
{
Console.WriteLine($"Virus {viruslist[i]}: {datesvirusstrench[viruslist[i]]} => {datestime[viruslist[i]]/3} seconds");
remeininghealth -=datestime[viruslist[i]]/3;
Console.WriteLine($"{viruslist[i]} defeated in {secondstominute(datestime[viruslist[i]]/3)}m {secondstoostseconds(datestime[viruslist[i]]/3)}s.");
Console.WriteLine($"Remeining health: {Math.Floor(remeininghealth)}");
}
remeininghealth = 1.20 * remeininghealth;
if(remeininghealth >= inicialhealth)
{
remeininghealth = inicialhealth;
}
}
Console.WriteLine($"Final Health: {Math.Floor(remeininghealth)}");
}
if (!(datesvirusstrench.ContainsKey(virusname)))
{
timetodefeat = virusstrench * virusname.Length;
timeminute = timetodefeat / 60;
timeseconds = timetodefeat % 60;
datesvirusstrench.Add(virusname, virusstrench);
datestime.Add(virusname, timetodefeat);
}
counter++;
}
}
static int secondstominute(int s)
{
int minute;
minute = s / 60;
return minute;
}
static int secondstoostseconds(int s)
{
int ost;
ost = s % 60;
return ost;
}
}
}
Да благодаря ти.Наистина не го бях съобразил.След корекцията ми даде 30/100.
Явно има още нещо за доизкусуряване.
Лек и успешен ден.