0

Okay, I am using Discord.net, creating a bot for discord(clearly), And I want to add a simple 'add default role' too the command handler, this is easily achieved, however, I want it to read from the (Configuration.json) file as we occasionally change the names, or want to add another role to the defaults, Keep in mind this is a private bot not a personal, and will only be in one server.

I am trying to use public List<string> Roles { get; set; }

When Configuration.SaveJSON(); is used, it saves it as null, this is okay for me, but when I try and add a role using the command, I get a nullreferenceexeption.

var Roley = new Configuration();
                    var roles = Roley.Roleys;
                    roles.Add("PSN");
                    Roley.SaveJson();

In my head, this should work, and now add 'PSN' to the list, but it doesn't, It gives me a nullreference, which I can't wrap my head around as I am trying to set it.

michaelukz
  • 27
  • 10
  • Roley.Roleys is null, you need to check for that and initialize it if it is null before adding to it. – Ron Beyer Apr 03 '18 at 00:01
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Camilo Terevinto Apr 03 '18 at 00:04
  • delete all the stuff in your question about discord, it adds no useful information to your question. – Keith Nicholas Apr 03 '18 at 01:47

1 Answers1

3

From what you have said, when it saves, it saves null. Meaning Roleys is null

which means the line roles.Add("PSN"); will throw a null exception

That means in your Configuration if you have something like

public List<string> Roleys {get; set; }

you need it to be

public List<string> Roleys {get; set; } = new List<string>();
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • any Idea why it is replacing the previous one as if its a string not a list? `config.Roleys.add` should work, yet instead its just replacing it. Either way, thanks for the response, it worked! – michaelukz Apr 03 '18 at 07:29
  • `roles.Add(role); Roley.SaveJson();` is the command, I earlier issued a statement `var roles = new Configuration(); var roles = roles.Roley` – michaelukz Apr 03 '18 at 08:04
  • forgot to mention, I am saving to a string, and the string is called 'role' – michaelukz Apr 03 '18 at 08:51
  • would really appreciate a response, I can't for the life of me figure it out! – michaelukz Apr 04 '18 at 10:03
  • I don't know if this is allowed (Posting links), but I can't think of a better way to send you code, since editing in the comments are just annoyingly hard and stupid, so here is a hastebin, with marks to say what it is and what is going on. Thanks for the help. https://hastebin.com/walevopexa.cs – michaelukz Apr 04 '18 at 22:41
  • oh right, looks like you are going new Configuration each time so you are recreating it every single time, adding a role. Then saving that. I'm not sure what your constructor in Configuration does.... but looks like you are blanking it every time – Keith Nicholas Apr 04 '18 at 23:01
  • I don't know how to save to it other wise, I just used the code edit the config that was in a tutorial 'change prefix' video, I thought that was the correct way to do it, how would I go about doing it then? – michaelukz Apr 04 '18 at 23:18
  • Hey man, I don't know if you forgot about this, but how would I reference the config so I don't have to create a new configuration, IE how can I avoid `var config = new configuration();` but still be able to do `config.roleys.Add("Discord Role");`. Thanks – michaelukz Apr 08 '18 at 21:28