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):
    org.bukkit.command.CommandException: Unhandled exception executing command 'testz' in plugin PurchaseMenuTest v1.0
        at org.bukkit.command.PluginCommand.execute( ~[spigot.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.command.SimpleCommandMap.dispatch( ~[spigot.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand( ~[spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand( [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a( [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a( [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a( [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$ [spigot.jar:git-Spigot-044d928-e8c6403]
        at java.util.concurrent.Executors$ [?:1.7.0_65]
        at [?:1.7.0_65]
        at net.minecraft.server.v1_8_R3.SystemUtils.a( [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B( [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B( [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A( [spigot.jar:git-Spigot-044d928-e8c6403]
        at [spigot.jar:git-Spigot-044d928-e8c6403]
        at [?:1.7.0_65]
    Caused by: java.lang.NullPointerException
        at me.dyenxunit.main.GUI.PurchaseGUI( ~[?:?]
        at me.dyenxunit.main.Main.onCommand( ~[?:?]
        at org.bukkit.command.PluginCommand.execute( ~[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):
  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<init>( ~[spigot.jar:git-Spigot-044d928-e8c6403]
        at ~[spigot.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin( ~[spigot.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins( [spigot.jar:git-Spigot-044d928-e8c6403]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins( [spigot.jar:git-Spigot-044d928-e8c6403]
        at net.minecraft.server.v1_8_R3.DedicatedServer.init( [spigot.jar:git-Spigot-044d928-e8c6403]
        at [spigot.jar:git-Spigot-044d928-e8c6403]
        at [?:1.7.0_65]
    Caused by: java.lang.IllegalAccessException: Class can not access a member of class me.dyenxunit.main.Main with modifiers "private"
        at sun.reflect.Reflection.ensureMemberAccess( ~[?:1.7.0_65]
        at java.lang.Class.newInstance( ~[?:1.7.0_65]
        at<init>( ~[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):
    • 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();

    // rest of the code