[Solved] getServer().getPlayer(name) is Null

zLeqitEclipse

New Member
Hi, im coding an Essentials Plugin. Right Now i am at the /gamemode command. The Command is working fine but, when i want to give another player gamemode (/gamemode 1 NAME) its giving me an NullPointerEX. Im fetching the target Player by:

Java:
Player target = mainClass.getServer().getPlayer(args[1]);
This is my exception in Console:

at de.zleqiteclipse.boxedessentials.Main.commands.gamemode.execute(gamemode.java:46)
at cn.nukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:257)
at cn.nukkit.Server.dispatchCommand(Server.java:764)
at cn.nukkit.Player.handleDataPacket(Player.java:2807)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at cn.nukkit.network.Network.processPackets(Network.java:268)
at cn.nukkit.network.Network.processBatch(Network.java:251)
at cn.nukkit.Player.handleDataPacket(Player.java:2103)
at cn.nukkit.network.RakNetInterface.handleEncapsulated(RakNetInterface.java:156)
at cn.nukkit.raknet.server.ServerHandler.handlePacket(ServerHandler.java:132)
at cn.nukkit.network.RakNetInterface.process(RakNetInterface.java:65)
at cn.nukkit.network.Network.processInterfaces(Network.java:152)
at cn.nukkit.Server.tick(Server.java:1137)
at cn.nukkit.Server.tickProcessor(Server.java:916)
at cn.nukkit.Server.start(Server.java:893)
at cn.nukkit.Server.<init>(Server.java:578)
at cn.nukkit.Nukkit.main(Nukkit.java:112)

> 03:23:59 [ERROR] Throwing
java.lang.NullPointerException: null
at de.zleqiteclipse.boxedessentials.Main.commands.gamemode.execute(gamemode.java:46) ~[?:?]
at cn.nukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:257) ~[server.jar:?]
at cn.nukkit.Server.dispatchCommand(Server.java:764) ~[server.jar:?]
at cn.nukkit.Player.handleDataPacket(Player.java:2807) ~[server.jar:?]
at java.util.ArrayList.forEach(ArrayList.java:1257) [?:1.8.0_242]
at cn.nukkit.network.Network.processPackets(Network.java:268) [server.jar:?]
at cn.nukkit.network.Network.processBatch(Network.java:251) [server.jar:?]
at cn.nukkit.Player.handleDataPacket(Player.java:2103) [server.jar:?]
at cn.nukkit.network.RakNetInterface.handleEncapsulated(RakNetInterface.java:156) [server.jar:?]
at cn.nukkit.raknet.server.ServerHandler.handlePacket(ServerHandler.java:132) [server.jar:?]
at cn.nukkit.network.RakNetInterface.process(RakNetInterface.java:65) [server.jar:?]
at cn.nukkit.network.Network.processInterfaces(Network.java:152) [server.jar:?]
at cn.nukkit.Server.tick(Server.java:1137) [server.jar:?]
at cn.nukkit.Server.tickProcessor(Server.java:916) [server.jar:?]
at cn.nukkit.Server.start(Server.java:893) [server.jar:?]
at cn.nukkit.Server.<init>(Server.java:578) [server.jar:?]
at cn.nukkit.Nukkit.main(Nukkit.java:112) [server.jar:?]

I hope anyone can help :)
 

CreeperFace

🇨🇿🇺🇦 Слава Україні!
Staff member
Hi, im coding an Essentials Plugin. Right Now i am at the /gamemode command. The Command is working fine but, when i want to give another player gamemode (/gamemode 1 NAME) its giving me an NullPointerEX. Im fetching the target Player by:

Java:
Player target = mainClass.getServer().getPlayer(args[1]);
This is my exception in Console:

at de.zleqiteclipse.boxedessentials.Main.commands.gamemode.execute(gamemode.java:46)
at cn.nukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:257)
at cn.nukkit.Server.dispatchCommand(Server.java:764)
at cn.nukkit.Player.handleDataPacket(Player.java:2807)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at cn.nukkit.network.Network.processPackets(Network.java:268)
at cn.nukkit.network.Network.processBatch(Network.java:251)
at cn.nukkit.Player.handleDataPacket(Player.java:2103)
at cn.nukkit.network.RakNetInterface.handleEncapsulated(RakNetInterface.java:156)
at cn.nukkit.raknet.server.ServerHandler.handlePacket(ServerHandler.java:132)
at cn.nukkit.network.RakNetInterface.process(RakNetInterface.java:65)
at cn.nukkit.network.Network.processInterfaces(Network.java:152)
at cn.nukkit.Server.tick(Server.java:1137)
at cn.nukkit.Server.tickProcessor(Server.java:916)
at cn.nukkit.Server.start(Server.java:893)
at cn.nukkit.Server.<init>(Server.java:578)
at cn.nukkit.Nukkit.main(Nukkit.java:112)

> 03:23:59 [ERROR] Throwing
java.lang.NullPointerException: null
at de.zleqiteclipse.boxedessentials.Main.commands.gamemode.execute(gamemode.java:46) ~[?:?]
at cn.nukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:257) ~[server.jar:?]
at cn.nukkit.Server.dispatchCommand(Server.java:764) ~[server.jar:?]
at cn.nukkit.Player.handleDataPacket(Player.java:2807) ~[server.jar:?]
at java.util.ArrayList.forEach(ArrayList.java:1257) [?:1.8.0_242]
at cn.nukkit.network.Network.processPackets(Network.java:268) [server.jar:?]
at cn.nukkit.network.Network.processBatch(Network.java:251) [server.jar:?]
at cn.nukkit.Player.handleDataPacket(Player.java:2103) [server.jar:?]
at cn.nukkit.network.RakNetInterface.handleEncapsulated(RakNetInterface.java:156) [server.jar:?]
at cn.nukkit.raknet.server.ServerHandler.handlePacket(ServerHandler.java:132) [server.jar:?]
at cn.nukkit.network.RakNetInterface.process(RakNetInterface.java:65) [server.jar:?]
at cn.nukkit.network.Network.processInterfaces(Network.java:152) [server.jar:?]
at cn.nukkit.Server.tick(Server.java:1137) [server.jar:?]
at cn.nukkit.Server.tickProcessor(Server.java:916) [server.jar:?]
at cn.nukkit.Server.start(Server.java:893) [server.jar:?]
at cn.nukkit.Server.<init>(Server.java:578) [server.jar:?]
at cn.nukkit.Nukkit.main(Nukkit.java:112) [server.jar:?]

I hope anyone can help :)
mainClass variable is null
 

zLeqitEclipse

New Member
So, i got it working. Here's what i did:

First on the top of the class i made an plugin variable:

Java:
private Main plugin;
Right under it i init the class:

Java:
   public gamemode(Main plugin) {
        this.plugin = plugin;
    }
And instead of mainClass, i used the plugin variable.

That's it.
 

DotCheck

New Member
lol You could have just done this.getServer().getPlayer() and that would've worked. Or getServer().getInstance().getPlayer().
Sorry for the necropost but this should help so you don't have to add all that.
 
Top