Help with my plugin

Discussion in 'Plugin Development' started by comexpert, May 20, 2013.

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

    comexpert

    Hello,

    I made a plugin for players so that they can have a backpack.
    But when I start my server to test it, it gives a error in the onEnable() command.

    OnEnable() :
    Code:
    public class Project extends JavaPlugin {
    String[] playerList;
    Inventory[] inv;
    Player player;
    Material[] matData;
    ItemStack[] workItem, dia;
    int i, id=0;
    String name;
     
    public void onEnable() {
    playerList[0]="...";
    getLogger().info("Hello");
    }
     
    // and some more code ...
    }
    
    Error in Console :

    Code:
    [SEVERE] Error occurred while enabling Project v1.0 (Is it up to date?)
    java.lang.NullPointerException
            at me.comexpert.Project.Project.onEnable(Project.java:23)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
            at org.bukkit.craftbukkit.v1_5_R3.CraftServer.loadPlugin(CraftServer.java:282)
            at org.bukkit.craftbukkit.v1_5_R3.CraftServer.enablePlugins(CraftServer.java:264)
            at net.minecraft.server.v1_5_R3.MinecraftServer.j(MinecraftServer.java:304)
            at net.minecraft.server.v1_5_R3.MinecraftServer.e(MinecraftServer.java:283)
            at net.minecraft.server.v1_5_R3.MinecraftServer.a(MinecraftServer.java:243)
            at net.minecraft.server.v1_5_R3.DedicatedServer.init(DedicatedServer.java:151)
            at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:382)
            at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    
    Do one of you know what the problem is ?

    Thanks,

    C0m_Expert99 (I know my name is stupid, but I named my name before making plugins :confused: )

    EDIT : PS : The plugin is not done yet, I was just asking about the onEnable() part !
     

    Attached Files:

  2. Offline

    caseif

  3. Offline

    comexpert

    I red that post, but I can't get what I do wrong ...

    Code:
    public class Project extends JavaPlugin {
     
    String[] playerList;
     
    public void onEnable() {
    playerList[0]="...";
    }
     
    
    It makes the String[] playerList before the onEnable() command, but still doesn't it work.

    Or is the onEnable() command called BEFORE the make of the String[] playerList ?
     
  4. Offline

    comexpert

  5. Offline

    afistofirony

    The array playerList was never initialised, you need to initialise it first:

    Code:
    String[] playerList = new String[Bukkit.getOnlinePlayers().length()]; // Create a String[] with as many slots as online players
     
    public void onEnable () {
        for (int i = 0; i < Bukkit.getOnlinePlayers().length(); i++) {
            playerList[i] = Bukkit.getOnlinePlayers()[i].getName();
        }
        playerList[0] = "...";
    }
    EDIT: It's worth noting that at this point this will also generate an ArrayIndexOutOfBoundsException with no online players. So your best bet is to create the String[] when it is needed.
     
  6. Offline

    comexpert

    Thanks, that worked ! ;)
     
Thread Status:
Not open for further replies.

Share This Page