Solved Problems

Discussion in 'Plugin Development' started by MordorKing78, Jan 1, 2015.

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

    MordorKing78

    So I have some problem with getting an public void from my Arena Manager. I'll post both of my classes (very small) can anyone tell me whats wrong?

    ARENA MANAGER:
    Code:
    package org.m.zSlayer.Main;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    public class ArenaManager {
        File AR;
        FileConfiguration ARENA;
     
        public Main plugin;
        
        public ArenaManager(Main plugin)
        {
            this.plugin = plugin;
        }
      
        public void createArena(String arena, int maxPlayers){
            AR = new File(plugin.getDataFolder(), "Data.yml");
            ARENA = YamlConfiguration.loadConfiguration(AR);
          
            String shortcut = "arenas." + arena + "."; //Shortcut
          
            ARENA.set(shortcut + "maxPlayers", maxPlayers);
         
            SaveNC();
          
        }
      
        public void SaveNC(){
            try {
                ARENA.save(AR);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    COMMAND:

    Code:
    package org.m.zSlayer.Commands;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.m.zSlayer.Main.ArenaManager;
    import org.m.zSlayer.Main.Main;
    
    public class slayer implements CommandExecutor{
        public Main plugin;
        
        public slayer(Main plugin)
        {
            this.plugin = plugin;
        }
      
        private ArenaManager am = new ArenaManager(plugin);
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player p = (Player)sender;
            p.sendMessage("Your arena is " + args[0]);
            am.createArena(args[0], 2);
          
            return false;
        }
    }
    
    ERROR:
    Code:
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at org.m.zSlayer.Commands.slayer.onCommand(slayer.java:22) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    Ingame it also gives an error ^_^
     
  2. Offline

    Dudemister1999

    Code:
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    This usually means you're trying to get something in an array that is empty.

    Error leads to:
    Code:
    p.sendMessage("Your arena is " + args[0]);
    Meaning there is no args[0]. args[] is empty.
     
  3. Offline

    MordorKing78

    @Dudemister1999 I tried it with an argument and I failed it yes. But it still does not work correctly.

    @Dudemister1999 new error:
    Code:
    Caused by: java.lang.NullPointerException
            at org.m.zSlayer.Main.ArenaManager.createArena(ArenaManager.java:21)
    :?]
            at org.m.zSlayer.Commands.slayer.onCommand(slayer.java:23) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  4. Offline

    Luke_Lax

    If you had read this then you would know that we need the lines from the respective line numbers.

    I'd advise you to give it a read as I see you post here a lot and it seems you're not able to debug your own code properly.
     
  5. Offline

    MordorKing78

    @Luke_Lax I think I know how to read them. But people always ask for an error. But I can't figure out what is wrong with the code. it seems okay?
     
  6. @MordorKing78
    AR = new File(plugin.getDataFolder(), "Data.yml");
    Your plugin data folder doesn`t exist, create one before casting it.
    if (!plugin.getDatafolder.exists) {
    plugin.getDataFolder.mkdir();
    }
     
    Regablith likes this.
  7. Offline

    MordorKing78

  8. Offline

    MordorKing78

    @Juancomaster1998 K. I'm going to try it now.

    @Juancomaster1998 Can't find exists

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  9. Offline

    NathanWolf

    Make sure you're using "getDataFolder()" - calling the method.
     
  10. Offline

    MordorKing78

    @Juancomaster1998 Oh it actually does. But the error is still there:
    Code:
    Caused by: java.lang.NullPointerException
            at org.m.zSlayer.Main.ArenaManager.createArena(ArenaManager.java:21)
    :?]
            at org.m.zSlayer.Commands.slayer.onCommand(slayer.java:23) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~
    @NathanWolf

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  11. Offline

    NathanWolf

    Code:
        public Main plugin;
    
        public slayer(Main plugin)
        {
            this.plugin = plugin;
        }
        private ArenaManager am = new ArenaManager(plugin);
    This is your problem, I think. The constructor will happen after the other two statements, meaning "plugin" is null when you create ArenaManager.

    Try moving the initialization of "am" to the constructor.

    EDIT: In general, I would avoid do anything but very simple initialization in your class definition. Anything that's not a primitive type should probably be initialized to null (or marked "final" and not initialized) and then created in your class constructor. This is generally good practice, at least in my opinion!
     
  12. Offline

    MordorKing78

    @NathanWolf How do you mean "am" am is just to call the class? Sorry but I'm very lost because I was just arlready struggeling with those lines.
     
  13. Offline

    NathanWolf

    "am" is what you named your ArenaManager instance (longer more descriptive variable names are often less confusing!)

    When you have a variable in a class (or a "field"), you have to declare it, as well as initialize it.

    You are doing both in the same place right now- at object initialization time, before your class constructor gets called. You also have a "plugin" variable, that you are initializing at constructor time- but you are using it to initialize "am".

    This all happens out of order:
    1. Object initialized
    2. "plugin" declared and initialized to null (the default).
    3. "am" declared and initialized to "new ArenaManager(plugin)" - where "plugin" is null
    4. "slayer" constructor called
    5. "plugin" is set to something (your main plugin instance)
    6. But it is too late, "am" is already initialized with a null plugin, which is what causes the error later on.
    There is an easy fix- split #3 up, don't initialize it until around #5, in the "slayer" constructor method. At that point, you have a valid non-null "plugin" instance to use.
     
    coldandtired likes this.
  14. Offline

    MordorKing78

    @NathanWolf Err.. I tried some ways but I still can't fix it. I do not understand some of your explanation too because english is not my main language.
     
  15. Offline

    NathanWolf

    Wellll, not to spoonfeed- but the code I quoted above, try changing to:

    Code:
    public final Main plugin;
        public slayer(Main plugin)
        {
            this.plugin = plugin;
            this.am = new ArenaManager(plugin);
        }
        private final ArenaManager am;
    Note the "new ArenaManager" line moved. That should be all it takes to fix this problem- but do try to understand why you have to make this change!
     
    MordorKing78 likes this.
  16. Offline

    MordorKing78

    @NathanWolf there is an red line under plugin (this.plugin)

    @NathanWolf Omg Dumb me.. Going to try it now

    @NathanWolf THANX dude it works! I have been looking so long for this. I want to give you this cake ([cake])

    @bwfcwalshy Suhrry fur tripple post D:
    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than triple posting.>
     
    Last edited: Jan 3, 2015
    NathanWolf likes this.
Thread Status:
Not open for further replies.

Share This Page