Solved ArrayList help

Discussion in 'Plugin Development' started by ShadowLAX, Jul 6, 2013.

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

    ShadowLAX

    Hi bukkit community, I am having a problem with the ArrayList methods for 1.5.2. I don't know what I am doing wrong, but It doesn't seem to log the players name and save it. When ever I try to call it, i throws an error in the console even though I set the ArrayList up correctly. Code below:

    Main Class:
    Code:java
    1. public class Main extends JavaPlugin {
    2.  
    3. public ArrayList<String> players = new ArrayList<String>();
    4.  
    5. public void onEnable() {
    6. getLogger().info("has been enabled!");
    7. getCommand("test").setExecutor(new TestExecutor());
    8. Bukkit.getPluginManager().registerEvents(new EventListener(), this);
    9. }
    10.  
    11. public void onDisable() {
    12. getLogger().info("has been diabled!");
    13. }
    14.  
    15. };


    Command Executor:
    Code:java
    1. public class TestExecutor implements CommandExecutor {
    2.  
    3. public Main plugin;
    4.  
    5. @Override
    6. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    7. if (cmd.getName().equalsIgnoreCase("test")) {
    8. if (sender instanceof Player) {
    9. Player player = (Player) sender;
    10. plugin.players.add(player.getName());
    11. } else {
    12. sender.sendMessage("Players only!");
    13. }
    14. }
    15. return true;
    16. }
    17. }


    Event Listener:
    Code:java
    1. public class EventListener implements Listener {
    2.  
    3.  
    4.  
    5. public Main plugin;
    6.  
    7.  
    8.  
    9. @EventHandler(priority = EventPriority.HIGH)
    10.  
    11. public void onPlayerDeath(PlayerDeathEvent e) {
    12.  
    13. Player player = (Player) e.getEntity();
    14.  
    15. if (plugin.players.contains(player.getName())) {
    16.  
    17. plugin.players.remove(player.getName());
    18.  
    19. Bukkit.broadcastMessage("[Test] This is a test!");
    20.  
    21. }
    22.  
    23. }
    24.  
    25. };


    Thank you for your time!
     
  2. Offline

    Joeisi

    Solved below.
     
  3. Offline

    Compressions

    ShadowLAX You cannot call a non static method from a different class without making an instance of the class. Add this to your other class.
    Code:
    public EventListener(Main instance) {
    this.plugin = instance;
    }
     
  4. Offline

    ShadowLAX

    Compressions What class do I put/replace this block of code in?
     
  5. Offline

    Joeisi

  6. Offline

    ShadowLAX

    I'm still getting an error from console.

    console error:
    Code:
    [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin Test v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
        at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_5_R3.PlayerConnection.handleCommand(PlayerConnection.java:971)
        at net.minecraft.server.v1_5_R3.PlayerConnection.chat(PlayerConnection.java:889)
        at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:846)
        at net.minecraft.server.v1_5_R3.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292)
        at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java:115)
        at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:581)
        at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
        at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
        at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
        at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
        at me.shadowlax.testproject.TestExecutor.onCommand(TestExecutor.java:17)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    
    I put in the code Compressions gave me, but It still is not working.

    Error is on line 17 of Test Executor Class:
    Code:java
    1. package me.shadowlax.testproject;
    2.  
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandExecutor;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7.  
    8. public class TestExecutor implements CommandExecutor {
    9.  
    10. public Main plugin;
    11.  
    12. @Override
    13. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    14. if (cmd.getName().equalsIgnoreCase("boom")) {
    15. if (sender instanceof Player) {
    16. Player player = (Player) sender;
    17. plugin.players.add(player.getName());
    18. } else {
    19. sender.sendMessage("Players only!");
    20. }
    21. }
    22. return true;
    23. }
    24. };
    25.  
    26.  


    Event Listener:
    Code:java
    1. package me.shadowlax.testproject;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.EventPriority;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.entity.PlayerDeathEvent;
    9.  
    10. public class EventListener implements Listener {
    11.  
    12. public EventListener(Main instance) {
    13. this.plugin = instance;
    14. }
    15.  
    16. public Main plugin;
    17.  
    18. @EventHandler(priority = EventPriority.HIGH)
    19. public void onPlayerDeath(PlayerDeathEvent e) {
    20. Player player = (Player) e.getEntity();
    21.  
    22. if (plugin.players.contains(player.getName())) {
    23. plugin.players.remove(player.getName());
    24. Bukkit.broadcastMessage("[Test] This is a test!");
    25. }
    26. }
    27. };


    EDIT: Added imports
     
  7. Offline

    ZeusAllMighty11 Retired Staff

    Just thought I'd say that Bukkit doesn't contain the List + ArrayList collections, those are actually a part of Java.
     
  8. Offline

    slayr288

    ShadowLAX
    Include your imports, or the lines are not correct. You wouldn't get an NPE on a bracket.
     
  9. Offline

    Compressions

    ShadowLAX Make sure you import java.util for your collections.
     
  10. Offline

    ShadowLAX

  11. Offline

    soulofw0lf

    plugin.players is your npe,
    change players in your Main class to public static and then just call it using Main.players
     
  12. Offline

    ShadowLAX

    Thank you all so much for your help! Now I can finally finish my plugin! :D
     
Thread Status:
Not open for further replies.

Share This Page