0

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;
        }

        }
zapl
  • 63,179
  • 10
  • 123
  • 154
J22929
  • 5
  • 2
  • Something in the line `if(cmd.getName().equalsIgnoreCase("pring") && otherResources.PermissionHandler.getPerm(p)){` is `null`. I have no idea what those things are in that line. – zapl Jun 27 '16 at 14:57
  • @zapl its checking if the player has permission and will only execute the command if the player has it – J22929 Jun 27 '16 at 15:00
  • What is line 16 of PermissionHandler.java? That's where the error is. – zapl Jun 27 '16 at 15:03
  • @zapl if(configGetter.getConfig().getList("permsgl").contains(p.getName())){ return true; – J22929 Jun 27 '16 at 15:04
  • Which piece of that is null? One of `configGetter`, `configGetter.getConfig()`, `configGetter.getConfig().getList("permsgl")` or `p`. – zapl Jun 27 '16 at 15:06
  • What is the permission node? – WilliamTaco Jun 27 '16 at 15:07
  • @zapl sorry? i didn't really understand that – J22929 Jun 27 '16 at 15:10
  • @WilliamTaco im using config.yml for perms because im making the plugin for someone else and he wanted it that way, not the permission.yml – J22929 Jun 27 '16 at 15:11

1 Answers1

0

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:

  1. Initialise the Main variable using a static method
  2. Refactoring your plugin structure to remove the static requirement
G. May
  • 66
  • 1
  • 4
  • thank you for your help! – J22929 Jun 27 '16 at 15:23
  • No problem! Happy to help :) – G. May Jun 27 '16 at 15:35
  • quick question: since i used config in a command i put public static void before the command, to remove all static uses of config, would i have to use just public void onCommand(etc..? – J22929 Jun 27 '16 at 16:05
  • Your use of static was in the `PermissionHandler` class. This is the class I'd recommend removing static from and changing to fix your code – G. May Jun 28 '16 at 07:02