2 errors? Stack-trace & ???

Discussion in 'Plugin Development' started by Noepje, Jul 27, 2012.

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

    Noepje

    Hi,

    I'm coding a plugin to teleport 8 people to different positions in a map/world.
    This is done with /startgame.

    But I have 2 errors, 1 when the plugin gets enabled, 1 when the command has been used.

    The codes:
    Onenable:
    Code:
    package me.noepje.semiautowalls;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class semiautowalls extends JavaPlugin {
    Location[] locations;
    @Override
    public void onEnable(){
    getLogger().info("Semiautowalls has been enabled.");
    World w =  Bukkit.getServer().getWorld("world");
    locations[0] = new Location(w, 284.5, 116.5, -735.5);
    locations[1] = new Location(w, 403.5, 116.5, -729.5);
    locations[2] = new Location(w, 410.5, 116.5, -848.5);
    locations[3] = new Location(w, 291.5, 116.5, -855.5);
    locations[4] = new Location(w, 291.5, 116.5, -729.5);
    locations[5] = new Location(w, 410.5, 116.5, -736.5);
    locations[6] = new Location(w, 403.5, 116.5, -855.5);
    locations[7] = new Location(w, 284.5, 116.5, -848.5);
    }
    So when it enables, the following error pops up in the console:
    Code:
    [SEVERE] Error occured while enabling semiautowalls v1.0 <Is it up to date?>
    java.lang.NullPointerException
    at me.noepje.semiautowalls.semiautowalls.onEnable<semiautowalls.java:20
    at org.bukkit.plugin.java.JavaPlugin.setEnabled<JavaPlugin.java:215>
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin<JavaPluginLoader.java:337>
    at org.bukkit.plugin.SimplePluginmanager.enablePlugin<SimplePluginmanager.java:381>
    at org.bukkit.craftbukkit.CraftServer.loadPlugin<CraftServer.java:256>
    at org.bukkit.craftbukkit.CraftServer.enablePlugins<CraftServer.java:238>
    at net.minecraft.server.MinecraftServer.t<MinecraftServer.java:281>
    at net.minecraft.server.MinecraftServer.a<MinecraftServer:368>
    at net.minecraft.server.MinecraftServer.init<MinecraftServer.java:197>
    at net.minecraft.server.MinecraftServer.run<MinecraftServer.java:432>
    at net.minecraft.server.ThreadServerApplication.run<SourceFile:492>
    
    So here out seems that the world thing gives the error, but I don't know why...

    2nd error:

    Code:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    if(cmd.getName().equalsIgnoreCase("startgame")){ // If the player typed /startgame then do the following...
     
    getServer().broadcastMessage(ChatColor.RED + "All the players have now been teleported to their spots.");
     
    for(int i = 0; i < getServer().getOnlinePlayers().length; i++){
     
    Player player = getServer().getOnlinePlayers()[i];
    player.teleport(locations[i]);
     
    }
     
    sender.sendMessage("People are now teleported to their spots");
    return true;
    } //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
    return false;
    }
    Error:
    Ingame error:
    [​IMG]

    In console:
    Code:
    [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'startgame' in plugin semiautowalls v1.0
    at org.bukkit.command.PluginCommand.execute<PluginCommand.java:42>
    at org.bukkit.command.SimpleCommandMap.dispatch<SimpleCommandMap.java:166>
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand<CraftServer.java:479>
    at net.minecraft.server.NetServerHandler.handleCommand<NetServerHandles.java:821>
    at net.minecraft.server.NetServerHandler.chat<NetServerHandler.java:781>
    at net.minecraft.server.NetServerHandler.a<NetServerHandler.java:764>
    at net.minecraft.server.Packet3Chat.handle<Packet3Chat.java:34>
    at net.minecraft.server.NetworkManager.b<NetWorkManager.java:229>
    at net.minecraft.server.NetServerHandler.a<NetServerHandler.java:113>
    at net.minecraft.server.NetworkListenThread.a<NetworkListenThread.java:78>
    at net.minecraft.server.MinecraftServer.w<MinecraftServer.java:567>
    at net.minecraft.server.MinecraftServer.run<MinecraftServer,java:459>
    at net.minecraft.server.ThreadServerApplication.run<SourceFile:492>
    Caused by: java.lang.NullPointerException
    at me.noepje.semiautowalls.semiautowalls.onCommand<semiautowalls.java:41>
    at org.bukkit.command.PluginCommand.execute<PluginCommand.java:40>
    ... 12 more
    
    So I heard this is a stack-trace, but I can't seem to fix it right :\
    And yes, I read http://forums.bukkit.org/threads/ho...ubleshoot-your-own-plugins-by-yourself.32457/

    And finally to be sure:
    plugin.yml
    Code:
    name: semiautowalls
    main: me.noepje.semiautowalls.semiautowalls
    version: 1.0
    commands:
      startgame:
        description: Teleports people to their spots
        usage: /<command>
        permission: semiautowalls.startgame
        permission-message: You don't have <permission>
    I would be grateful if someone knew how to make this work!
     
  2. Offline

    Jeff.Halbert

    well, i think the second error may be cause because you are defining your locations inside of your onEnable(), which would make them local to that object. so when you call on your location inside of the onCommand object, there is nothing there....

    =EDIT=
    i'm a bit unsure because I can't see the line numbers on the code, so there's no way of tellign which line is #41
     
  3. Offline

    Noepje

    Line #41 is
    Code:
    player.teleport(locations[i]);
     
  4. Offline

    Xemos

    What in your plugin.yml ? im pretty sure you got "/startgame" correct? then your oncommand should check for 0 args IE

    Code:
     
     
    if (args.length == 0) {
    doStuff();
    return true;
    }
    
     
  5. Offline

    Jeff.Halbert

    if you dont want your locations to ever change, declare them as constants outside of your objects....

    Code:
    public class className extends JavaPlugin() {
      private static final Location locs[] = {new Location(1,2,3), new Loc...};
      //then you can use them in objects....
      public void onPlayerJoin(PlayerJoinEvent event) {
        event.getPlayer().setLocation(locs[1]);
        // or whatever, might not be an actual method lol
        // but you get my drift?
      }
    }
    if you want them to change, i would suggest a hashmap..
     
  6. Offline

    Noepje

    Is there something wrong with how I make the location[0] etc?
    Because that is where the error is...

    I tried that code, but it still gives me the error...

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

    CorrieKay

    youre getting a null pointer exception on enable because "locations" isnt initialized.

    which, im going to go out on a limb, is why youre getting the second exception.

    Also, that explains why that code is giving you the error, as youre trying to access a null variable.

    Initialize your array :p
     
Thread Status:
Not open for further replies.

Share This Page