I'm making a bukkit plugin and am using a config.yml and have a boolean to access the config, but since i am referring to it in another class, it has to be static which i think is causing it to break. Not sure how to fix.
Error messages:
at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
7:28:03 AM at java.lang.Thread.run(Thread.java:745) [?:1.8.0_65]
7:28:03 AM Caused by: java.lang.NullPointerException
7:28:03 AM at otherResources.PermissionHandler.getPerm(PermissionHandler.java:16) ~[?:?]
7:28:03 AM at main.Main.onCommand(Main.java:33) ~[?:?]
7:28:03 AM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
The code:
package main;
import java.util.Arrays;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import otherResources.PermissionHandler;
public class Main extends JavaPlugin{
public void onEnable(){
new PermissionHandler(this);
getLogger().info("Green lantern class is now active.");
this.getConfig().addDefault("permsgl", "");
this.getConfig().options().copyDefaults(true);
saveConfig();
}
public void onDisable(){
getLogger().info("Green lantern class is not active.");
saveConfig();
}
@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
Player p = (Player) sender;
if(cmd.getName().equalsIgnoreCase("pring") && otherResources.PermissionHandler.getPerm(p)){
ItemStack PowerRing = new ItemStack(Material.SLIME_BALL);
ItemMeta PowerRingMeta = PowerRing.getItemMeta();
PowerRingMeta.setDisplayName(ChatColor.GREEN + "Power Ring");
PowerRingMeta.setLore(Arrays.asList(ChatColor.DARK_GREEN + "Mode: Laser"));
p.getInventory().addItem(PowerRing);
Bukkit.broadcastMessage("Spawn Ring is in Order");
return true;
}
if(cmd.getName().equalsIgnoreCase("gladd") && (p.isOp())){
Player t = Bukkit.getServer().getPlayer(args[1]);
otherResources.PermissionHandler.givePerm(t);
Bukkit.broadcastMessage("Spawn Ring is in Order");
if(!t.isOnline()){
p.sendMessage(ChatColor.RED + "ERROR! " + ChatColor.YELLOW + args[1] + " is either offline or does not exist." );
return true;
}
}
else{
return true;
}
return true;
}
}
If you look at your error message:
7:28:03 AM Caused by: java.lang.NullPointerException
7:28:03 AM at otherResources.PermissionHandler.getPerm(PermissionHandler.java:16) ~[?:?]
7:28:03 AM at main.Main.onCommand(Main.java:33) ~[?:?]
7:28:03 AM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
In your code, you're calling something on an object which is null
It's occurring here:
otherResources.PermissionHandler.getPerm(p) // line 33
because you are calling getPerm
statically, like you said, and:
You assign your main variable in the constructor by doing this (post was edited, so the second class was removed):
public PermissionHandler(Main plugin) {
configGetter = plugin; // Assigning inside constructor
}
but then when you create the object, you don't use the variable:
public void onEnable(){
new PermissionHandler(this); // You create it, but don't assign it to a value, or use the value
}
So you are creating an instance of an object, but you are not using it, and you are calling a static method, meaning it has no knowledge of the variable.
In terms of fixing your problem:
but since i am referring to it in another class, it has to be static which i think is causing it to break. Not sure how to fix.
The simplest solution is to use a Singleton Design Pattern. It allows you to create a single instance of an object (so it will let you assign that Main variable) whilst providing a global reference to that object (i.e. you can use it like a static variable). I'd recommend reading up on it.
Other solutions: