C
C#4w ago
merlo17

Null reference exception

I am attempting to make a discord bot, and when I run a command like "?hello" I get a NullReferenceException. _commands was null. So I attempted to fix it by initializing _commands in the startup by adding:
private CommandService _commands;
private IServiceProvider _services:

public async Task InitializeAsync()
{
_commands = new CommandService();
_services = new ServiceCollection()
.AddSingleton(_commands)
.BuildServiceProvider();

await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
}
private CommandService _commands;
private IServiceProvider _services:

public async Task InitializeAsync()
{
_commands = new CommandService();
_services = new ServiceCollection()
.AddSingleton(_commands)
.BuildServiceProvider();

await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
}
and then making sure to call the InitializeAsync() in the Main task. However, now I get error CS1503: Argument 1: cannot convert from System.Reflection.Assembly to System.Type. I'm a little stumped on this and can't really find any details on what should be in place of Assembly.GetEntryAssembly()
No description
4 Replies
merlo17
merlo17OP4w ago
Here's a full view of my Program.cs which serves as the startup script
No description
merlo17
merlo17OP4w ago
and my CommandHandler.cs where I'm getting the NullReferenceExcpetion
No description
Angius
Angius4w ago
Where does HandleCommandAsync() get _commands from? And cannot convert from System.Reflection.Assembly to System.Type. is quite clear It needs a type, not an assembly
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?