[Solved] Cannot get Task to work

This is the error I get:
[23:21:03] [Server thread/ERROR] [Sponge]: Error occurred while executing command ‘getuser Lmmb74’ for source DedicatedServer: plugin
java.lang.NullPointerException: plugin
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:229) ~[minecraft_server.1.10.2.jar:?]
at org.spongepowered.common.scheduler.SpongeScheduler.checkPluginInstance(SpongeScheduler.java:158) ~[SpongeScheduler.class:1.10.2-2171-5.1.0-BETA-1992]
at org.spongepowered.common.scheduler.SpongeTaskBuilder.submit(SpongeTaskBuilder.java:104) ~[SpongeTaskBuilder.class:1.10.2-2171-5.1.0-BETA-1992]
at com.namelessmc.namelessplugin.sponge.commands.GetUserCommand.execute(GetUserCommand.java:148) ~[GetUserCommand.class:?]
at org.spongepowered.api.command.spec.CommandSpec.process(CommandSpec.java:351) ~[CommandSpec.class:1.10.2-2171-5.1.0-BETA-1992]
at org.spongepowered.api.command.dispatcher.SimpleDispatcher.process(SimpleDispatcher.java:333) ~[SimpleDispatcher.class:1.10.2-2171-5.1.0-BETA-1992]
at org.spongepowered.common.command.SpongeCommandManager.process(SpongeCommandManager.java:291) [SpongeCommandManager.class:1.10.2-2171-5.1.0-BETA-1992]
at net.minecraft.command.ServerCommandManager.func_71556_a(SourceFile:83) [bd.class:?]
at net.minecraft.server.dedicated.DedicatedServer.func_71333_ah(DedicatedServer.java:423) [ld.class:?]
at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:388) [ld.class:?]
at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:613) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:471) [MinecraftServer.class:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]

I try to use runnables in the commands but this is what I get. The source code is here:

Pls help u.u

Your plugin variable isn’t set to anything, thus returning you with a NullPointerException.

Hopefully I fixed it with this Pull Request

I am gonna test it now, because at first I used your method but I got the same error.

It’s a very simple error if you know how to read a stack trace.

This means that a reference called plugin is null.

Since this is the only element that mentions your code, the problem must be here, in GetUserCommand at line 148.
https://github.com/NamelessMC/Nameless-Plugin/blob/master/nameless-sponge/src/main/java/com/namelessmc/namelessplugin/sponge/commands/GetUserCommand.java#L148

You can see here that the line references something called plugin, which the error says is null.
https://github.com/NamelessMC/Nameless-Plugin/blob/master/nameless-sponge/src/main/java/com/namelessmc/namelessplugin/sponge/commands/GetUserCommand.java#L33

And here’s where it’s defined; you create it, but never assign anything to it. There’s the problem; you need to put a plugin in plugin.

Is this the correct way of fixing it? Commit

No, because statics are bad.
:stuck_out_tongue: Technically it’d work, but I would always recommend a constructor (or even injector) pattern instead of a statics pattern.’

i.e.

public GetUserCommand(NamelessPlugin plugin) {
    this.plugin = plugin;
}

Ok. But it seems to work, so thanks :smiley:

Ok, gonna do that then. But also thanks @rojo8399 :smiley:

@pie_flavor Forgot about that method. Using that from now on.

@Lmmb74 Happy to help! Too bad it wasn’t the right way :stuck_out_tongue:

Actually, what I would do myself (if I ever bothered with creating multiple classes :stuck_out_tongue: ) is @Inject the Injector, and then use getInstance to construct the class. Then I can just @Inject my main plugin class in the command executor class or what-have-you.