So I've got this class with two lists right now. One is getting initialized and one is not and I don't understand why. someList
is just there, because I thought I am too stupid to initialize a list, but someList
works just fine.
Here's my class:
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
class Calendar
{
static List<CalendarNode> _calendar = new List<CalendarNode>();
static List<int> someList = new List<int>();
public static int addNode(CalendarNode node)
{
if (_calendar != null)
{
foreach (CalendarNode item in _calendar)
{
if (item.Username == node.Username)
{
return 1;
}
}
}
someList.Add(1); // works like a charm
_calendar.Add(node); // throws Error Object reference not set to an instance of an object
}
}
What am I doing wrong?
Thanks for your help
edit1:
Regex pattern = new Regex(@"\.0000");
DateTime date;
IUserMessage message;
CalendarNode node = new CalendarNode();
try
{
if (pattern.Match(birthday).Success)
{
Regex replace = new Regex(@"\d{4}");
birthday = replace.Replace(birthday, "1900");
date = DateTime.ParseExact(birthday, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
}
else
{
date = DateTime.ParseExact(birthday, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
}
node = new CalendarNode(Context.User.Mention, date);
}
catch (Exception)
{
message = await Context.Channel.SendMessageAsync($"Probably invalid date! Use format: \"dd.mm.yyyy\"\nIf the error persists, contact Nigolasy");
}
int status = 2;
if (node != null)
{
status = Calendar.addNode(node);
}
This is the code where I call my addNode.
And my CalendarNode class, in case it is important
class CalendarNode
{
string username;
DateTime birthdate;
public CalendarNode(string username, DateTime birthdate)
{
this.username = username;
this.birthdate = birthdate;
}
public CalendarNode(){ }
public string Username { get { return username; } }
public DateTime Birthdate { get { return birthdate; } }
}