NullPointerException

Discussion in 'Spigot Plugin Development' started by Mystical_Mage, Jun 10, 2016.

Thread Status:
Not open for further replies.
  1. Hello, when I try to load my plugin on my testing server, it fails when it tries to load my command giving a NullPointerException, I just don't understand what hasn't been initialized:
    Code (Java):

    public class AreaDetector extends JavaPlugin{
        private AreaDetector plugin;
        public static FileConfiguration config = new FileConfiguration() {
         
            @Override
            public String saveToString() {
                // TODO Auto-generated method stub
                return null;
            }
         
            @Override
            public void loadFromString(String arg0) throws InvalidConfigurationException {
                // TODO Auto-generated method stub
             
            }
         
            @Override
            protected String buildHeader() {
                // TODO Auto-generated method stub
                return null;
            }
        };
        String[] Hello;
        @Override
        public void onEnable() {
            config.addDefault("Yav", Hello);
            config.addDefault("REQ", Hello);
            config.addDefault("Ara", Hello);
            Object[] value = {};
            config.addDefault("Points", value);
            config.options().copyDefaults(true);
            System.out.println("[King of the Hill]Added Default configs");
            this.getCommand("createcp").setExecutor(new CreateCPoint());
            System.out.println("[King of the Hill]Command createcp loaded");
    And here is the error
    Code (Text):

    [18:19:02 INFO]: [King_of_the_Hill] Enabling King_of_the_Hill v0.1
    [18:19:02 INFO]: [King of the Hill]Added Default configs
    [18:19:02 ERROR]: Error occurred while enabling King_of_the_Hill v0.1 (Is it up to date?)
    java.lang.NullPointerException
            at plugin.mysticalmage.koth.AreaDetector.onEnable(AreaDetector.java:47) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) ~[spigot-1.9.2.jar:git-Spigot-e000104-4cb3258]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.9.2.jar:git-Spigot-e000104-4cb3258]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.9.2.jar:git-Spigot-e000104-4cb3258]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.loadPlugin(CraftServer.java:361) [spigot-1.9.2.jar:git-Spigot-e000104-4cb3258]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.enablePlugins(CraftServer.java:321) [spigot-1.9.2.jar:git-Spigot-e000104-4cb3258]
            at net.minecraft.server.v1_9_R1.MinecraftServer.t(MinecraftServer.java:411) [spigot-1.9.2.jar:git-Spigot-e000104-4cb3258]
            at net.minecraft.server.v1_9_R1.MinecraftServer.l(MinecraftServer.java:376) [spigot-1.9.2.jar:git-Spigot-e000104-4cb3258]
            at net.minecraft.server.v1_9_R1.MinecraftServer.a(MinecraftServer.java:331) [spigot-1.9.2.jar:git-Spigot-e000104-4cb3258]
            at net.minecraft.server.v1_9_R1.DedicatedServer.init(DedicatedServer.java:269) [spigot-1.9.2.jar:git-Spigot-e000104-4cb3258]
            at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:527) [spigot-1.9.2.jar:git-Spigot-e000104-4cb3258]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    PS: Sorry if i'm doing something wrong or am ignoring a good practice, I'm new to spigot
     
    #1 Mystical_Mage, Jun 10, 2016
    Last edited: Jun 11, 2016
  2. Hello doesn't have a value.
     
    • Optimistic Optimistic x 1
  3. Wait, that wasn't it... still get the error even when I gave Hello a value:
    Code (Java):
    Object[] Hello = {};
     
  4. Replace calls to config with getConfig()
     
  5. The nullpointer (According to the error if I am interpreting it correctly) is on line 47 which is:
    Code (Java):
    this.getCommand("createcp").setExecutor(new CreateCPoint());
     
  6. But when I do that it gives errors on my command class about how it "Cannot make a static reference to the non-static field AreaDetector.config"
    Edit: Added the command class
    Code (Java):

    package plugin.mysticalmage.koth;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class CreateCPoint implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (args[0] != null) {
                return false;
            }
            String name = args[0];
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (player.hasPermission("controlpoint.add")) {
                    PotionEffect Invs = new PotionEffect(PotionEffectType.INVISIBILITY, 9999, 199, false, false);
                    PotionEffect Regen = new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 9999, 199, false, false);
                    player.getWorld().spawnEntity(player.getLocation(), EntityType.ARMOR_STAND);
                    java.util.List<Entity> armor = player.getNearbyEntities(0.01, 0.01, 0.01);
                    for (int i = 0; i > armor.size(); i++) {
                        if (armor.get(i) instanceof ArmorStand) {
                            Invs.apply((LivingEntity) armor.get(i));
                            Regen.apply((LivingEntity) armor.get(i));
                            AreaDetector.config.set(name, (armor.get(i).getUniqueId())); //The issue is with the AreaDetector.config
                            Object[] Points = AreaDetector.config.getList("Points").toArray();
                            Points[Points.length] = name;
                            AreaDetector.config.set("Points", Points);
                        }
                    }
                }
                else {
                    return false;
                }
            }
            else {
                return false;
            }
            return true;
        }
    }
     
    #6 Mystical_Mage, Jun 10, 2016
    Last edited: Jun 10, 2016
  7. [
    Code (Java):
    Object[] value = {};[/I] // ????
    Everything has @Override..? Are you sure you know what that even does?


    Stop trying to make plugins, go learn java..
     
  8. JavaPlugin gives you a config, you don't need to make a config object yourself. (this.getConfig())
    Also I'm not sure where you saw this, but I don't think it works:
    Code (Text):

    publicstatic FileConfiguration config =new FileConfiguration(){
     
     
  9. Do you mean in this section?:
    Code (Java):
       publicstatic FileConfiguration config =new FileConfiguration(){
           
            @Override
           public[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+string']String[/URL] saveToString(){
               // TODO Auto-generated method stub
               returnnull;
           }
           
            @Override
           publicvoid loadFromString([URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+string']String[/URL] arg0)throws InvalidConfigurationException {
               // TODO Auto-generated method stub
               
           }
           
            @Override
           protected[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+string']String[/URL] buildHeader(){
               // TODO Auto-generated method stub
               returnnull;
           }
       };
    Because I've replaced that based off of what Redreid said:
    With this:
    Code (Java):
    public FileConfiguration config = getConfig();
    PS: Am I doing any of this right, or should I just start over?
     
  10. At the top of your onEnable you need to put

    Code (Text):

        @Override
       publicvoid onEnable(){
      plugin = this;
     
    But more importantly, you need to learn more about java and the Spigot API.

    NullPointerExceptions are often the easiest error to fix, because it usually means you said "here's an object, now do stuff with it." without ever giving it a value to work with.
     
  11. I am not even sure what you are trying to do, your code is giving me cthulhu vibes.

    Starting over (multiple times) as you learn is a good idea, but if you have no previous programming experience (by experience i mean a year or more, not a 5-part youtube class), you really should start with generic java before you even try making plugins, or you will miss a lot and make a lot of mistakes and poor optimization.
     
    • Funny Funny x 1
  12. kottalizer

    Supporter

    • Like Like x 1
    • Agree Agree x 1
  13. Just as a note, you guys could have a better attitude than "This person is a dumbo who doesn't know Java", I'm sure you have your reasons from past experience, but could you at least try to understand? I fixed my plugin just now and it wasn't because i'm a dumbo who can't do anything, it was a mistake. I know most of this doesn't matter to you that much, but could you guys try to be kind to somebody who was following the Wiki (Which is wrong FYI) and made some mistakes. I know you don't care, but think about what is being asked next time instead of defaulting to thinking of the person who asked as "Another idiot who doesn't know Java".

    </rant>
     
Thread Status:
Not open for further replies.