[SOLVED]java.lang.IllegalArgumentException: File cannot be null

Discussion in 'Plugin Development' started by diiPex, Apr 3, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    diiPex

    *This is The first plugin I've made, I am a newb, help!*
    Alright so I finished my very first plugin yesterday, everything was working great, but I tried to add a configuration file and I am now getting this error when I start the server:
    Code:
    182 recipes
    27 achievements
    19:02:20 [INFO] Starting minecraft server version 1.2.4
    19:02:20 [INFO] Loading properties
    19:02:20 [INFO] Starting Minecraft server on *:25565
    19:02:20 [WARNING] **** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!
    19:02:20 [WARNING] The server will make no attempt to authenticate usernames. Be
    ware.
    19:02:20 [WARNING] While this makes the game possible to play without internet a
    ccess, it also opens up the ability for hackers to connect with any username the
    y choose.
    19:02:20 [WARNING] To change this, set "online-mode" to "true" in the server.pro
    perties file.
    19:02:20 [INFO] This server is running CraftBukkit version git-Bukkit-1.2.4-R1.0
    -b2126jnks (MC: 1.2.4) (Implementing API version 1.2.4-R1.0)
    19:02:20 [SEVERE] Could not load 'plugins\ArcherElite.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:148)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:305)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:230)
            at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:207)
            at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:183)
            at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigur
    ationManager.java:53)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:156)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:425)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
    Caused by: java.lang.NullPointerException
            at me.diiPex.ArcherElite.EntityListener.<init>(EntityListener.java:19)
            at me.diiPex.ArcherElite.ArcherElite.<init>(ArcherElite.java:11)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
     
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
    rce)
            at java.lang.reflect.Constructor.newInstance(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:144)
            ... 8 more
    19:02:20 [INFO] Preparing level "world"
    19:02:20 [INFO] Default game type: 1
    19:02:20 [INFO] Preparing start region for level 0 (Seed: -6748377539439689033)
    19:02:21 [INFO] Preparing start region for level 1 (Seed: -6748377539439689033)
    19:02:21 [WARNING] ----- Bukkit Auto Updater -----
    19:02:21 [WARNING] Your version of CraftBukkit is out of date. Version 1.2.5-R1.
    0 (build #2149) was released on Wed Apr 04 14:06:18 EDT 2012.
    19:02:21 [WARNING] Details: http://dl.bukkit.org/downloads/craftbukkit/view/0102
    6_1.2.5-R1.0/
    19:02:21 [WARNING] Download: http://dl.bukkit.org/downloads/craftbukkit/get/0102
    6_1.2.5-R1.0/craftbukkit.jar
    19:02:21 [WARNING] ----- ------------------- -----
    19:02:21 [INFO] Preparing spawn area: 16%
    19:02:22 [INFO] Preparing start region for level 2 (Seed: -6748377539439689033)
    19:02:22 [INFO] Server permissions file permissions.yml is empty, ignoring it
    19:02:22 [INFO] Done (2.007s)! For help, type "help" or "?"
    >
    This is my Main class:
    Code:
    package me.diiPex.ArcherElite;
     
     
    import java.util.logging.Logger;
     
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class ArcherElite extends JavaPlugin {
    public static Logger log = Logger.getLogger("Minecraft");
    public final EntityListener EntityListener = new EntityListener();
     
     
    public void loadConfiguration(){
     
     
    getConfig().addDefault("CriticalHit.LongRange.Enabled", true);
    getConfig().addDefault("CriticalHit.LongRange.Range", 25);
    getConfig().addDefault("CriticalHit.LongRange.DamageMultiplier", 1.5);
    getConfig().addDefault("CriticalHit.LeatherBonus.Enabled", true);
    getConfig().addDefault("CriticalHit.LeatherBonus.DamageMultiplier", 1.5);
    getConfig().addDefault("CriticalHit.LongRange+LeatherBonus.Enabled", true);
    getConfig().addDefault("CriticalHit.LongRange+LeatherBonus.DamageMultiplier", 2);
     
     
        getConfig().options().copyDefaults(true); 
        saveConfig();
        
    }
     
    @Override
    public void onDisable() {
            log.info("ArcherElite v1.1.0 has been disabled!");
        }
    @Override
        public void onEnable() {
       loadConfiguration();
       PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.EntityListener, this);
            log.info("ArcherElite v1.1.0 has been enabled!");
            
     
    }
     
        
     
     
    }
     
     
    
    This is my Entity Listener:
    Code:
    package me.diiPex.ArcherElite;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
     
     
    public class EntityListener implements Listener{
    private ArcherElite plugin;
    public boolean LRE = plugin.getConfig().getBoolean("CriticalHit.LongRange.Enabled");
    public double LRR = plugin.getConfig().getDouble("CriticalHit.LongRange.Range");
    public double LRM = plugin.getConfig().getDouble("CriticalHit.LongRange.Multiplier");
    public boolean LBE = plugin.getConfig().getBoolean("CriticalHit.LeatherBonus.Enabled");
    public boolean LRandLB = plugin.getConfig().getBoolean("CriticalHit.LongRange+LeatherBonus.Enabled");
    public double LBM = plugin.getConfig().getDouble("CriticalHit.LeatherBonus.DamageMultiplier");
    public double LRandLBM = plugin.getConfig().getDouble("CriticalHit.LongRange+LeatherBonus.DamageMultiplier");
     
     
     
    @EventHandler
    //Critical hit + Leather Bonus damage modifiers 
    public void onEntityDamage(EntityDamageEvent event) {
    if(event instanceof EntityDamageByEntityEvent && event.getCause() == DamageCause.PROJECTILE){
    EntityDamageByEntityEvent ee = (EntityDamageByEntityEvent)event;
    Projectile projectile =(Projectile) ee.getDamager();
    if(projectile.getShooter() instanceof Player){
    Player player = (Player) projectile.getShooter();
                    if(ee.getEntity() instanceof LivingEntity){
                        LivingEntity damagee = (LivingEntity) ee.getEntity();
                        double distance = player.getEyeLocation().distance(damagee.getEyeLocation());
                        PlayerInventory inv = player.getInventory();
                        if (inv.getHelmet() != null) {
                            ItemStack playerHelmet = inv.getHelmet();
                        if (inv.getChestplate() != null) {
                            ItemStack playerChestplate = inv.getChestplate();
                        if (inv.getLeggings() != null) {
                            ItemStack playerLeggings = inv.getLeggings();
                        if (inv.getBoots() != null) {
                            ItemStack playerBoots = inv.getBoots();
                            
                            Material LeatherHelmet = Material.LEATHER_HELMET; 
                            Material LeatherChestplate = Material.LEATHER_CHESTPLATE;
                            Material LeatherLeggings = Material.LEATHER_LEGGINGS; 
                            Material LeatherBoots = Material.LEATHER_BOOTS;
                            
                        if (distance >= LRR && playerHelmet.getType() == LeatherHelmet && playerChestplate.getType() == LeatherChestplate && playerLeggings.getType() == LeatherLeggings && playerBoots.getType() == LeatherBoots){
                       if (LRandLB = true){
                       
                       double finaldamage1 = (ee.getDamage() * LRandLBM);
                       ee.setDamage((int) finaldamage1);
                       player.sendMessage(ChatColor.GRAY + "[ArcherElite]: " + ChatColor.GOLD + "Critical hit! " + ChatColor.RED + "(LRandLBMx)");
                       ee.setCancelled(false);}
                                              
                        }else if (distance < LRR && playerHelmet.getType() == LeatherHelmet && playerChestplate.getType() == LeatherChestplate && playerLeggings.getType() == LeatherLeggings && playerBoots.getType() == LeatherBoots){
                       if (LBE = true){
                       
                       double finaldamage3 = (ee.getDamage() * LBM);
                       ee.setDamage((int) finaldamage3);
                       player.sendMessage(ChatColor.GRAY + "[ArcherElite]: " + ChatColor.GOLD + "Critical hit! " + ChatColor.RED + "(LBMx)");
                       ee.setCancelled(false);}
                                }                        
                            }
                       }
                       }
                        }
                    }
    }
    }
    }
     
     
    @EventHandler
    public void onEntityDamage1(EntityDamageEvent event1) {
    if(event1 instanceof EntityDamageByEntityEvent && event1.getCause() == DamageCause.PROJECTILE){
    EntityDamageByEntityEvent ee1 = (EntityDamageByEntityEvent)event1;
    Projectile projectile1 =(Projectile) ee1.getDamager();
    if(projectile1.getShooter() instanceof Player){
    Player player1 = (Player) projectile1.getShooter();
                    if(ee1.getEntity() instanceof LivingEntity){
                        LivingEntity damagee1 = (LivingEntity) ee1.getEntity();
                        double distance1 = player1.getEyeLocation().distance(damagee1.getEyeLocation());
                        
                        PlayerInventory inv1 = player1.getInventory();
                        if (LRE = true){
                        if (inv1.getHelmet() == null) {
                        
                       if (distance1 >= LRR){
                       double finaldamage1 = (ee1.getDamage() * LRM);
                       ee1.setDamage((int) finaldamage1);
                       player1.sendMessage(ChatColor.GRAY + "[ArcherElite]: " + ChatColor.GOLD + "Critical hit! " + ChatColor.RED + "(LRMx)");
                       ee1.setCancelled(false);}
                    
                       
                       }if (inv1.getChestplate() == null) { 
                       
                       if (distance1 >= LRR){
                       double finaldamage1 = (ee1.getDamage() * LRM);
                       ee1.setDamage((int) finaldamage1);
                       player1.sendMessage(ChatColor.GRAY + "[ArcherElite]: " + ChatColor.GOLD + "Critical hit! " + ChatColor.RED + "(LRMx)");
                       ee1.setCancelled(false);}
                    
                       
                       }if (inv1.getLeggings() == null) { 
                       
                       if (distance1 >= LRR){
                       double finaldamage1 = (ee1.getDamage() * LRM);
                       ee1.setDamage((int) finaldamage1);
                       player1.sendMessage(ChatColor.GRAY + "[ArcherElite]: " + ChatColor.GOLD + "Critical hit! " + ChatColor.RED + "(LRMx)");
                       ee1.setCancelled(false);}
                      
                       
                        
                       }if (inv1.getBoots() == null) {  
                       
                       if (distance1 >= LRR){
                       double finaldamage1 = (ee1.getDamage() * LRM);
                       ee1.setDamage((int) finaldamage1);
                       player1.sendMessage(ChatColor.GRAY + "[ArcherElite]: " + ChatColor.GOLD + "Critical hit! " + ChatColor.RED + "(LRMx)");
                       ee1.setCancelled(false);}}
                       
                       if (inv1.getHelmet() != null) {
                                ItemStack playerHelmet1 = inv1.getHelmet();
                            if (inv1.getChestplate() != null) {
                                ItemStack playerChestplate1 = inv1.getChestplate();
                            if (inv1.getLeggings() != null) {
                                ItemStack playerLeggings1 = inv1.getLeggings();
                            if (inv1.getBoots() != null) {
                                ItemStack playerBoots1 = inv1.getBoots();
                       
                       Material LeatherHelmet1 = Material.LEATHER_HELMET; 
                            Material LeatherChestplate1 = Material.LEATHER_CHESTPLATE;
                            Material LeatherLeggings1 = Material.LEATHER_LEGGINGS; 
                            Material LeatherBoots1 = Material.LEATHER_BOOTS;
                            
                            
                        if (playerHelmet1.getType() != LeatherHelmet1 || playerChestplate1.getType() != LeatherChestplate1 || playerLeggings1.getType() != LeatherLeggings1 || playerBoots1.getType() != LeatherBoots1){
                       if (distance1 >= LRR){
                                
                           double finaldamage1 = (ee1.getDamage() * LRM);
                           ee1.setDamage((int) finaldamage1);
                           player1.sendMessage(ChatColor.GRAY + "[ArcherElite]: " + ChatColor.GOLD + "Critical hit! " + ChatColor.RED + "(LRMx)");
                           ee1.setCancelled(false);
                        }
                        }
                        }
                        }
                        }
                        }
                        }
                    }
    }
    }
    }
    }
                       
                      
    I Believe the problem is with the main class, not the entity listener, but I could be wrong.
    Please help XD
     
  2. Offline

    CorrieKay

    configuration doesnt get set (internally) until onEnable is called. set your configuration inside onEnable() ^ ^
     
    diiPex likes this.
  3. Offline

    diiPex

    The configuration is set inside onEnable():
    If I'm missing something let me know XD

    Code:
    public void onEnable() {
       
            try{
     
            File ArcherElite = new File (getDataFolder() + "config.yml");
            FileConfiguration config = getConfig();
       
            if(!config.contains("CriticalHit.LongRange.Enabled")){
            config.set("CriticalHit.LongRange.Enabled", true);
            }
            if(!config.contains("CriticalHit.LongRange.Range")){
            config.set("CriticalHit.LongRange.Range", 25);
            }
            if(!config.contains("CriticalHit.LongRange.DamageMultiplier")){
            config.set("CriticalHit.LongRange.DamageMultiplier", 1.5);       
            }       
            if(!config.contains("CriticalHit.LeatherBonus.Enabled")){
              config.set("CriticalHit.LeatherBonus.Enabled", true);
            }
            if(!config.contains("CriticalHit.LeatherBonus.DamageMultiplier")){
            config.set("CriticalHit.LeatherBonus.DamageMultiplier", 1.5);       
            }
            if(!config.contains("CriticalHit.LongRange+LeatherBonus.Enabled")){
            config.set("CriticalHit.LongRange+LeatherBonus.Enabled", true);       
            }       
            if(!config.contains("CriticalHit.LongRange+LeatherBonus.DamageMultiplier")){
            config.set("CriticalHit.LongRange+LeatherBonus.DamageMultiplier", 2);
            }
                       
    saveConfig();
    }catch(Exception e1){
    e1.printStackTrace();
    }
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.EntityListener, this);
            log.info("ArcherElite v1.1.0 has been enabled!");
     
       
     
     
            }
     
        }
     
  4. Offline

    CorrieKay

    Code:
    public class ArcherElite extends JavaPlugin {
        public static Logger log = Logger.getLogger("Minecraft");
        public final EntityListener EntityListener = new EntityListener();
        FileConfiguration config = getConfig();
    fourth line, just declare it, dont try to set it.
     
  5. Offline

    diiPex

    How would I do that? Sorry I'm very new to this.
     
  6. Offline

    CorrieKay

    wait wat. Why are you loading your default configuration into a new configuration? (lines 51-76)

    You dont need to do that, bukkit already does that for you.

    The reason youre getting a file cannot be null error is because the method getConfig() (which you are using during class creation, before onEnable is run, on line 14) is returning a null file..

    you technically do grab your file configuration again in onEnable, however you dont need to load it yourself. just use getConfig instead of loading it from file manually
     
    diiPex likes this.
  7. Offline

    diiPex

    Alright I see where I'm getting my problem. My problem is I cant use getConfig() outside of onEnable(), but I also cant make public variables inside onEnable(), so how am i supposed to get the values from inside my config into my other class (EntityListener)?
     
  8. Offline

    CorrieKay

    nono, you can use getConfig outside of onEnable, but the function call has to originate from after onEnable is called.

    For instance, in a function called by onEnable, or in an event listener, or onCommand.

    If you wish to create a listener that can handle your configuration, there are two ways you can do this.

    1) create the listener from within your onEnable function. This is where you typically would register the listener anyways, so unless you actually need to pass the listener around (maybe it has functions in it that you need to call elsewhere) you would declare the listener before onEnable() (by doing YourListener yourListener; then in onEnable, doing yourListener = new YourListener(parameters); ) Once you do this, you can pass your configuration or plugin (which can be used to grab the configuration later) into the listeners constructor as a parameter. once there, in the listener you'd set it as one of the object varibles. Using this method, you dont actually have to create the listener inside onEnable, you can create it outside.

    2) you can do exactly what 1 says, BUT, instead of passing the plugin as a parameter, you can pass your configuration in as a parameter. This only works inside onEnable, because the configuration will not be null anymore, unlike in the first one, where you pass the PLUGIN as a parameter, then after onEnable, it accesses the config.

    Thats probably really confusing, so just do this:

    create your listener and pass the plugin into it (YourListener listener = new YourListener(this); )

    then inside of your listener, have it save the plugin instance, and access the config by doing: (pluginInstance.getConfig(); )
     
    diiPex likes this.
  9. Offline

    diiPex

    I Believe I have done what you said correctly, but I am still getting an error. (I updated my main post with the new error message and new classes)
     
  10. Offline

    CorrieKay

    aye, the problem is youre creating the entity listener before onEnable. I know i said that you can do this, but what i mean is that you cannot cannot cannot use getConfig before onEnable, no matter what.

    Instead of setting your variables (im talking about your listener now) = getConfig().something

    just declare them, then put the initialization inside of a method, and call that method from within onEnable().
     
    diiPex likes this.
  11. Offline

    diiPex

    But I cant make public variables or change a public variable inside of a method.

    I think I'm misunderstanding something, could you possibly write a short example to avoid mis communication. Thanks for your help, I'm sure you know what your doing, I'm just really dumb XD
     
  12. Offline

    CorrieKay

    Code:
    public EntityListener EntityListener;
     
    @Override
        public void onEnable() {
       loadConfiguration();
       PluginManager pm = getServer().getPluginManager();
    EntityListener = new EntityListener();
            pm.registerEvents(this.EntityListener, this);
            log.info("ArcherElite v1.1.0 has been enabled!");
    do this instead, that should fix your error ^ ^
     
    diiPex likes this.
  13. Offline

    diiPex

    Code:
    [INFO] ArcherElite v1.1.0 has been enabled!
    YES! Thank you sooooooo much Corrie!
    What you said above (in the quote) was NOT what solved the problem , but what you said in an earlier post, that i didn't understand, was however.
    What I did was state the variables before onEnable(), than inside onEnable() use getConfig() to state their value:

    Code:
    public EntityListener EntityListener;   
       
        public static boolean LRE;
        public static double LRR;
        public static double LRM;
        public static boolean LBE;
        public static boolean LRandLB;
        public static double LBM;
        public static double LRandLBM;
       
        @Override
            public void onEnable() {
           
          loadConfiguration();
          PluginManager pm = getServer().getPluginManager();
        EntityListener = new EntityListener();
                pm.registerEvents(this.EntityListener, this);
                log.info("ArcherElite v1.1.0 has been enabled!");
             
                LRE = getConfig().getBoolean("CriticalHit.LongRange.Enabled");
                LRR = getConfig().getDouble("CriticalHit.LongRange.Range");
                LRM = getConfig().getDouble("CriticalHit.LongRange.Multiplier");
                LBE = getConfig().getBoolean("CriticalHit.LeatherBonus.Enabled");
                LRandLB = getConfig().getBoolean("CriticalHit.LongRange+LeatherBonus.Enabled");
                LBM = getConfig().getDouble("CriticalHit.LeatherBonus.DamageMultiplier");
                LRandLBM = getConfig().getDouble("CriticalHit.LongRange+LeatherBonus.DamageMultiplier");
    }
    
    Thank you so much for your help, I finally have a working config! :D
     
    CorrieKay likes this.
Thread Status:
Not open for further replies.

Share This Page