1

So I've been re-programming my discord bot lately to make it more compliant with the discord Terms of Service. During my reprogramming, I've been attempting to make my bot more resistant to the programming errors I'm bound to include, as I'm merely a novice in C#. My bot was working completely fine yesterday and I've had no major code changes, but as of late I've been getting this error message upon any intensive use.

    System.NullReferenceException was unhandled
Message: An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll
Additional information: Object reference not set to an instance of an object.

Now, I don't know much about exceptions and object oriented programming, but I do know this; Why am I getting an error in a library I'm including?

Full code for the bot in a pastebin below.

https://pastebin.com/88tAFbK8

Zomon333
  • 69
  • 1
  • 3
  • 9
  • 4
    You haven't come remotely close to providing us enough information. NullRef exceptions are probably the most generic exception you could possibly have and they can happen on almost any line of code – Joe Phillips Jul 23 '17 at 17:29

1 Answers1

0

It looks Java style code. There are a lot of anonymous methods and it is difficult to debug such code. I recommend separate anonymous functions from the constructor. I think Visual Studio will stop on the problematic place while nullreferenceexception happens. If not, place on several places System.Diagnostics.Debug.WriteLine("your string or even object from the code"); and look it in output window, which is possible to open from Ctrl+Alt+A and type View.OutputWindow (Unfortunately now I'm using linux and I can't check my comment). In this way you will be able to determine what is going into the code, without stopping it.

What about NullReferenceException, It happens when your are accessing object function or property or any field of the object, which is not pointing to any object in the memory. e.g. from your code, if in the constructor discord is not initialized it can throw an exception, on the second line, where you are trying to access discord.UsingCommands...

Anyway, NullReferenceException is very general and it is hard to answer.

zviad
  • 586
  • 7
  • 18