ArrayList Question

Discussion in 'Spigot Plugin Development' started by dyenxunit, Jun 28, 2015.

  1. Hi, I was wondering how can i make an array list be accessed from multiple classes without being static, I have tried having a getter in my class that i want to access the array list in, here is the code but i keep getting an error when i try to do Main.plugin.PlayerOn.contains(p.getName()){}

    Code (Text):
     private Main plugin;
       
         public GUI(Main instance) {
             plugin = instance;
             Bukkit.getPluginManager().registerEvents(this, plugin);
         }
    Code (Text):
     null
    org.bukkit.command.CommandException: Unhandled exception executing command 'testz' in plugin PurchaseMenuTest v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:642) ~[spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1135) [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:970) [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-044d928-e8c6403]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_65]
        at java.util.concurrent.FutureTask.run(FutureTask.java:262) [?:1.7.0_65]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:718) [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot.jar:git-Spigot-044d928-e8c6403]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_65]
    Caused by: java.lang.NullPointerException
        at me.dyenxunit.main.GUI.PurchaseGUI(GUI.java:25) ~[?:?]
        at me.dyenxunit.main.Main.onCommand(Main.java:40) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-044d928-e8c6403]
        ... 15 more
     
     
  2. You could do this:

    Code (Text):

    private ArrayList<Object> list = new ArrayList<>();
    private YourClassName() {}
    private static YourClassName instance = new YourClassName();

    public static YourClassName getInstance() {
      return instance;
    }
     
    And then get the ArrayList like this:

    Code (Text):
    YourClassName.getInstance().list
     
  3. Doesnt work i tested the constructor and it said that it cannot be private
     
  4. Or just use 'this.list'
     
  5. How do i make that constructor work do i just change it to public ?
     
  6. What constructor?
     
  7. The one that marknzl posted
     
  8. Oh, didn't notice that, but it shouldn't have to be public since you are only using it in that class.
     
  9. Oh ok
     
  10. I bumped into an error using that constructor

    Code (Text):
    org.bukkit.plugin.InvalidPluginException: No public constructor
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:78) ~[spigot.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [spigot.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:291) [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:198) [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:528) [spigot.jar:git-Spigot-044d928-e8c6403]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_65]
    Caused by: java.lang.IllegalAccessException: Class org.bukkit.plugin.java.PluginClassLoader can not access a member of class me.dyenxunit.main.Main with modifiers "private"
        at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:109) ~[?:1.7.0_65]
        at java.lang.Class.newInstance(Class.java:368) ~[?:1.7.0_65]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot.jar:git-Spigot-044d928-e8c6403]
        ... 7 more

    Code (Text):
    private Main() {}
    private static Main instance = new Main();
    public ArrayList<String> Kit = new ArrayList<String>();

    public static Main getInstance(){
            return instance;
        }

    Code (Text):
    Main.getInstance().Kit
     
    • Agree Agree x 1
  11. You actually already did that in your first post example code, as far as I can tell
     
  12. It keeps generating an error though
     
  13. Could you post your current code?

    Whole classes I mean
     
  14. Code (Java):
    public class Main extends JavaPlugin {
    private List<String> list = new ArrayList<>();

    public void onEnable() {
    // register commands & events
    }

    public List<String> getList() {
    return list;
    }

    Code (Java):
    public class GUI {
    private Main instance;

    public GUI(Main plugin) {
    instance = plugin;
    }

    // example method
    public void openGUI(Player player) {
    // some code

    List<String> list = instance.getList();
    list.add(player.getName());

    // rest of the code
    }

    }