InvocativeTargetException and config problems help

Discussion in 'Plugin Development' started by Jogy34, Aug 26, 2011.

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

    Jogy34

    I'm creating a plugin and for part of it I need to, when a player clicks a sign, input the player name in a config file(or another file if there is another one that would work better) like:
    Code:
    <Player name>: <ability>
    EX:
    Jogy34: fire
    and then read from it to check to see if the player has the right ability and if not then it outputs a message. My biggest problem right now is how do I read from the file and should I be using a file format other than config for this

    EDIT: go down to my 8th post for the InvocativeTargetException

    anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  2. Offline

    bergerkiller

    Just in case you start handling these nodes on the fly (in onMove, oninteract) you should not read from the file at runtime. Instead try to use a hashmap with an arraylist in it, containing the abilities:
    Code:
    public static HashMap<String, ArrayList<String>> abilities = new HashMap<String, ArrayList<String>>();
    public static ArrayList<String> getAbilities(String playername) {
        ArrayList<String> rval = abilities.get(playername);
        if (rval == null) {
            rval = new ArrayList<String>();
            abilities.put(playername, rval);
        }
        return rval;
    }
    And add abilities in-code.
    Code:
    getAbilities("jogy").add("fire");
    Load this hashmap in onEnalbe and save it in onDisable. Here some classes I made, but people will probably dislike them. (hey, it is better than having two to three try-catches in one function!)

    SafeReader
    SafeWriter
     
  3. Offline

    Jogy34

    @bergerkiller Sorry but I don't really use hash maps so I have a few questions.
    1. how do I load/save them?
    2.If the player loges off would they still be in the hashmap when they log back on?
    3.would it still be the same if I tried to add people from from diffrent classes and could I use getPlayerName or something similar?

    EDIT: I still need to know how to read from a config file for other things like turning some abilities on/off
     
  4. Offline

    bergerkiller

    Configuration config = getConfiguration();
    //get something
    double value = config.getDouble("location.x", 20); //<< 20 is the default value if not found!
    //set something
    config.setProperty("location.x", value);
    //save it
    config.save();
     
  5. Offline

    Jogy34

    @bergerkiller that's writing to a config file but how do I read from one
     
  6. Offline

    bergerkiller

    Reading and writing is done 'on the fly'. You basically get and set what you want, and use save() to confirm your changes.
     
  7. Offline

    Jogy34

    @bergerkiller I misread that but I still have some questions about hashmaps
    1. how do I load/save them?
    2.If the player loges off would they still be in the hashmap when they log back on?
    3.would it still be the same if I tried to add people from from diffrent classes and could I use getPlayerName or something similar?
     
  8. Offline

    bergerkiller

    1.
    Store one list of players stored and read it based on that. For example:
    Writing
    Code:
    ArrayList<String> storedPlayers = new ArrayList<String>();
    for (String key : abilities.keyset) {
        storedPlayers.add(key);
    }
    config.setProperty("storedplayers", storedPlayers);
    //store the data
    for (String player : storedPlayers) {
        config.setProperty("ability." + player, abilities.get(player));
    }
    //save
    config.save();
    
    Loading
    Code:
    ArrayList<String> storedPlayers = config.getStringList("storedplayers", new ArrayList<String>());
    for (String player : storedPlayers) {
        abilities.put(player, config.getStringList("ability." + player, new ArrayList<String>());
    }
    2.
    Yes, data is stored separate from the Player object

    3.
    Yes, you can use player.getName() and get/set properties based on that. Once a player changes their name you will have to change the abilities manually, though. It is a key<>value relationship.
     
  9. Offline

    Jogy34

    @bergerkiller Here is what I have so far but there are a few errors:
    Code:
    public static MainRSC plugin;
        public Configuration config;
        public Boolean firePowerOnOff;
    
        public static HashMap<String, ArrayList<String>> abilities = new HashMap<String, ArrayList<String>>();
        public static ArrayList<String> getAbilities(String playername) {
            ArrayList<String> rval = abilities.get(playername);
            if (rval == null) {
                rval = new ArrayList<String>();
                parents.put(playername, rval);
            }
            return rval;
        }
        ArrayList<String> storedPlayers = new ArrayList<String>();{
        for (String key : abilities.keyset)[ERROR:abilities.keyset cannot be resolved or is not a field] {
            storedPlayers.add(key);
        }
        config.setProperty("storedplayers", storedPlayers);
        //store the data
        for (String player : storedPlayers) {
            config.setProperty("ability." + player, abilities.get(player));
        }
        }
        public final Logger logger = Logger.getLogger("Minecraft");
        private final RSCPlayerListener playerListener = new RSCPlayerListener(this);
    
        @Override
        public void onDisable() {
            this.logger.info("RSC has been disabled");
            config.save();
    
        }
    
        @Override
        public void onEnable() {
            this.logger.info("RSC has been enabled");
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvent(Event.Type.PLAYER_INTERACT, this.playerListener, Event.Priority.Normal, this);
            config = getConfiguration();
            firePowerOnOff = config.getBoolean("Fire", true);
            config.save();
        }
        public void reloadConfig() {
            config.setProperty("Boolean", firePowerOnOff);
            config.save();
            config = new Configuration(new File(getDataFolder().getPath() + "plugins/RSC/config.yml"));
            config.load();
            firePowerOnOff = config.getBoolean("Boolean", true);
            List<String> storedPlayers = config.getStringList("storedplayers", new ArrayList<String>());
            for (String player : storedPlayers) {
                parents.put [ERROR:The method put(String, ArrayList<String>) in the type
     HashMap<String,ArrayList<String>> is not applicable for the arguments (String,
     List<String>)(player, config.getStringList]("ability." + player, new ArrayList<String>());
            }
        }
    }
     
  10. Offline

    bergerkiller

    Change it to abilities.put or rename abilities to parents everywhere.

    Code:
        ArrayList<String> storedPlayers = new ArrayList<String>();{
        for (String key : abilities.keyset)[ERROR:abilities.keyset cannot be resolved or is not a field] {
            storedPlayers.add(key);
        }
        config.setProperty("storedplayers", storedPlayers);
        //store the data
        for (String player : storedPlayers) {
            config.setProperty("ability." + player, abilities.get(player));
        }
    Should be ketSet(). Also, it needs to be placed into a function/void. (static), or being added in onEnable.
     
  11. Offline

    Jogy34

    @bergerkiller Thanks but when I try to put something in the file from a diffrent class I get an invocationTargetException but I think that I'm getting the file wrong

    Main class:
    Code:
    public static MainRSC plugin;
        public Configuration config;
        public Boolean firePowerOnOff;
    
        public static HashMap<String, ArrayList<String>> abilities = new HashMap<String, ArrayList<String>>();
        public static ArrayList<String> getAbilities(String playername) {
            ArrayList<String> rval = abilities.get(playername);
            if (rval == null) {
                rval = new ArrayList<String>();
                abilities.put(playername, rval);
            }
            return rval;
        }
        public final Logger logger = Logger.getLogger("Minecraft");
        private final RSCPlayerListener playerListener = new RSCPlayerListener(this);
    
        @Override
        public void onDisable() {
            this.logger.info("RSC has been disabled");
            config.save();
    
        }
    
        @Override
        public void onEnable() {
            this.logger.info("RSC has been enabled");
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvent(Event.Type.PLAYER_INTERACT, this.playerListener, Event.Priority.Normal, this);
            config = getConfiguration();
            firePowerOnOff = config.getBoolean("Fire", true);
            config.save();
            ArrayList<String> storedPlayers = new ArrayList<String>();{
                for (String key : abilities.keySet()) {
                    storedAbilities.add(key);
                }
                config.setProperty("storedplayers", storedPlayers);
                for (String player : storedPlayers) {
                    config.setProperty("Ability" + player, parents.get(player));
                }
                }
        }
        public void reloadConfig() {
            //Save
            config.setProperty("Boolean", firePowerOnOff);
            config.save();
            //Reload
            config = new Configuration(new File(getDataFolder().getPath() + "plugins/RSC/config.yml"));
            config.load();
            firePowerOnOff = config.getBoolean("Boolean", true);
            List<String> storedPlayers = config.getStringList("storedplayers", new ArrayList<String>());
            for (String player : storedPlayers) {
                abilities.put(player, (ArrayList<String>) config.getStringList("ability." + player, new ArrayList<String>()));
            }
        }
    }
    My playerListener:
    Code:
    public class RSCPlayerListener extends PlayerListener {
        public static MainRSC plugin;
        public Configuration config;{
        config.load();
        }
    
        public String ability;
        public boolean playernme;
    
        public RSCPlayerListener(MainRSC instance) {
            plugin = instance;
        }
        Random rnd = new Random();
        public final Logger logger = Logger.getLogger("Minecraft");
    
            @Override
            public void onPlayerInteract(PlayerInteractEvent event){
                if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
                    Player player = event.getPlayer();
                    Block block = event.getClickedBlock();
                    BlockState state = block.getState();
                    Sign sign = (Sign)state;
                    String line0 = sign.getLine(0);
                    String line1 = sign.getLine(1);
                    if(line0.equals("[get]")){
                        if(line1.equals("ability")){
                            int r = rnd.nextInt(1);
                            if (r == 0){
                                player.sendMessage("You have been given the power of fire");
                                config.setProperty(player + ": Ability", "Fire");
                            }
                            if (r == 1){
                                player.sendMessage("You have been given the power of water");
                                config.setProperty(player + ": Ability", "Water");
                            }
                        }
                    }
                }
            }
    }
     
  12. Offline

    bergerkiller

    I don't think you understand it...

    Code:
    public Configuration config;
    
    public void onEnable() {
        config = this.getConfiguration();
        //Load from the damm config
        double d = config.getDouble("a.damm.key", defaultvalue);
    }
    
    public void onDisable() {
        //save the damm config
        config.setProperty("a.damm.key", value);
        config.save();
    }
    It's all you have to do. I see no saving in ondisable?
     
  13. Offline

    Jogy34

    I changed onDisable to:
    Code:
    @Override
    public void onDisable() {
    		this.logger.info("RSC has been disabled");
    		config.setProperty("Fire", false);
    		ArrayList<String> storedPlayers = new ArrayList<String>();{
    		for (String key : parents.keySet()) {
        	    storedPlayers.add(key);
        	}
        	config.setProperty("storedplayers", storedPlayers);
        	for (String player : storedPlayers) {
        	    config.setProperty("Parent" + player, parents.get(player));
        	}
    		config.save();
    		}
    	}
    and onEnable to:
    Code:
    @Override
    	public void onEnable() {
    		this.logger.info("RSC has been enabled");
    		PluginManager pm = getServer().getPluginManager();
    		pm.registerEvent(Event.Type.PLAYER_INTERACT, this.playerListener, Event.Priority.Normal, this);
    		config = getConfiguration();
    		firePowerOnOff = config.getBoolean("Fire", true);
    	    config.save();
    	}
    and I still get the same error:
    Code:
    17:17:26 [SEVERE] Could not load 'plugins\RSC.jar' in folder 'plugins':
    java.lang.reflect.InvocationTargetException
            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:173)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:213)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:136)
            at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:130)
            at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:97)
            at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigur
    ationManager.java:51)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:133)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:337)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    Caused by: java.lang.NullPointerException
            at me.Jogy34.RSC.RSCPlayerListener.<init>(RSCPlayerListener.java:19)
            at me.Jogy34.RSC.MainRSC.<init>(MainRSC.java:30)
    
    EDIT: I still think that it has something to do with me getting the config file wrong in my playerListener but I dont know how to fix it.

    anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  14. Offline

    krinsdeath

    You're trying to run config.load() on a field that hasn't been instantiated. This is an InvocationTargetException.

    Move config.load to your onEnable(), and if you need the configuration options in your RSCPlayerListener, move your instantiation of that field to your onEnable() as well.
     
  15. Offline

    Jogy34

    @krinsdeath
    how do I do that
     
  16. Offline

    Jogy34

    Anyone?

    anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  17. Offline

    Jogy34

    anyone?

    Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  18. Offline

    Jogy34

    cmon anyone?

    :'(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  19. If you could post the code and stack trace it would help a ton.
     
  20. Offline

    Jogy34

    @Adamki11s I have the code posted and what do you mean by stack trace
     
  21. The error it gives you in the console which shows the line numbers, cause etc.
     
  22. Offline

    Jogy34

    That is one of the reasons that i'm stuck, it doesnt give me a line:
    Code:
    17:17:26 [SEVERE] Could not load 'plugins\RSC.jar' in folder 'plugins':
    java.lang.reflect.InvocationTargetException
            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:173)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:213)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:136)
            at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:130)
            at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:97)
            at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigur
    ationManager.java:51)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:133)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:337)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    Caused by: java.lang.NullPointerException
            at me.Jogy34.RSC.RSCPlayerListener.<init>(RSCPlayerListener.java:19)
            at me.Jogy34.RSC.MainRSC.<init>(MainRSC.java:30)
     
  23. This might be happening because your trying to invoke methods which haven't been created / don't exist yet.

    public void onEnable() { this.logger.info("RSC has been enabled"); PluginManager pm = getServer().getPluginManager(); pm.registerEvent(Event.Type.PLAYER_INTERACT, this.playerListener, Event.Priority.Normal, this); config = getConfiguration();
    config.load(); firePowerOnOff = config.getBoolean("Fire", true); config.save(); }

    I would suggest you move everything highlighted in red and green to an external function and call it after on enable. Make sure to pass the configuration variable though.
     
  24. Offline

    Jogy34

    @Adamki11s So like this?
    Code:
    public static cofigFile(){
    config = getConfiguration();
    config.load();
    firePowerOnOff = config.getBoolean("Fire", true);
    config.save();
    }
    
    public void onEnable() {
    this.logger.info("RSC has been enabled");
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvent(Event.Type.PLAYER_INTERACT, this.playerListener, Event.Priority.Normal, this);
    cofigFile();
    }
    
     
  25. No, I edited it for you
     
  26. Offline

    Jogy34

    @Adamki11s That didn't work. I think I might be getting the config file wrong in my player listener.
     
  27. Offline

    nisovin

    I'm not sure why nobody has noticed this, but you're getting a NullPointerException, not a InvocationTargetException. That second exception is actually coming from the server plugin loader, the exception your plugin is getting is in the "Caused by" section. Your issue is this part:

    Code:
        public Configuration config;{
        config.load();
        }
    
    You're trying to run load() on a config variable that has no value. You'll need to set that value before you can load it. First remove the part with the braces. Then, you probably want something like this in your listener's constructor:
    Code:
    config = instance.getConfiguration();
    
     
  28. Offline

    Jogy34

    @nisovin that didn't work because if I try to use instance.getConfiguration it needs to be in the public RSCPlayerListener(MainRSC instance) method and if I put it in there then I cant use config.load() after it and if I pu the initialization of it in there then I cant retrieve the variable out of there. I'm hopelessly confused :(
     
  29. Offline

    krinsdeath

    The NullPointerException is being thrown because the method doesn't exist; he's invoking methods and fields which aren't instantiated (ie null), and thus the InvocationTargetException.

    To fix this:

    Code:
    private Configuration config; // the configuration
    private PluginManager pm; // your plugin manager instance
    private MyPlayerListener playerListener; // your player listener
    public void onEnable() {
      // get the configuration
      this.config = this.getConfiguration();
      // capture the plugin manager
      this.pm = this.getServer().getPluginManager();
      // instantiate the listener
      this.playerListener = new MyPlayerListener(this);
      // register the event
      this.pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener, Event.Priority.Normal, this);
      this.getServer().getLogger().info(String.valueOf(this + " is now enabled."));
    }
    
    I typically don't approve of writing plugins for people, but yeah. This is what you need to do.
     
  30. Offline

    Jogy34

    that still didn't work :(

    :'(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
Thread Status:
Not open for further replies.

Share This Page