NullPointerException when checking ArrayList size

Discussion in 'Plugin Development' started by KungFuGoat, May 7, 2014.

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

    KungFuGoat

    Hello,

    My ArrayList is in another class, i'm if the size of the arraylist is a specific number. When I do this I get a NullPointerException. I created an instance of the class, i'm not sure if this is the correct way to do it.

    This is the bit of my code that checks the ArrayList: http://pastebin.com/gn71WYmd

    And i'm not sure if this is correct but: http://pastebin.com/KpEJ24UJ

    One is for my main class and one is for the class containing the ArrayList
     
  2. Offline

    minoneer

    What does your arena class look like? Maybe you forgot to initialize your Player list when creating a new arena object?
     
  3. Offline

    KungFuGoat


    import java.util.ArrayList;

    public class PvPPluginArena {

    public ArrayList<String> players = new ArrayList<String>();

    public ArrayList<String> blue = new ArrayList<String>();

    public ArrayList<String> red = new ArrayList<String>();



    }

    That's all I got currently
     
  4. Offline

    xTigerRebornx

    KungFuGoat Can you show where you initialize the "arenaClass" variable?
     
  5. Offline

    KungFuGoat


    I don't think I have :C
    How would I do that?
     
  6. Offline

    xTigerRebornx

    KungFuGoat Depends, what is your "arenaClass" variable? Can you show where you declare it?
     
  7. Offline

    KungFuGoat


    In the PvPPluginManager class
     
  8. Offline

    minoneer

    You have 2 different Constructors for your PvPPluginManager-Class. One takes a plugin, the other one the PvPArenaClass. I'm assuming you create the manager with the constructor taking the plugin - hence the instance variable "arenaClass" is never assigned a value. That's why it's giving you a nullpointer when you try to use it.
     
    xTigerRebornx likes this.
  9. Offline

    xTigerRebornx

    KungFuGoat Like minoneer said, since you have 2 constructors for your PvPPluginManager class, there is a chance the one that defined the PvpPluginArena is never called. Instead of using 2 constructors, pass in both variables through a single constructor, allowing use of both without the chance of them not being declared like you currently have setup
     
  10. Offline

    KungFuGoat


    How would I use the ArrayList in the manager class then?
     
  11. Offline

    minoneer

    The exact same way you are doing now.

    Your current code:

    Code:java
    1.  
    2.  
    3. public PvPPluginArena arenaClass;
    4.  
    5. public PvPPluginManager(PvPPluginArena arenaClass){
    6.  
    7. this.arenaClass = arenaClass;
    8.  
    9. }
    10.  
    11. public PvPPlugin plugin;
    12.  
    13. public PvPPluginManager(PvPPlugin plugin){
    14.  
    15. this.plugin = plugin;
    16.  
    17. }
    18.  
    19.  



    What you should do:

    Code:java
    1.  
    2.  
    3. public PvPPlugin plugin;
    4. public PvPPluginArena arenaClass;
    5.  
    6. public PvPPluginManager(PvPPluginArena arenaClass, PvPPlugin plugin) {
    7. this.plugin = plugin;
    8. this.arenaClass = arenaClass;
    9.  
    10. }
    11.  
    12.  
     
  12. Offline

    KungFuGoat


    Thank you :) I appreciate it


    One more thing, how would I register this then?

    PvPPluginManager ppm = new PvPPluginManager(this);
    this.getServer().getPluginManager().registerEvents(ppm, this);
    this.getCommand("setarena").setExecutor(ppm);

    This is what I had before, but now eclipse is giving me a The constructor PvPPluginManager(PvPPlugin) is undefined

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  13. Offline

    minoneer

    Of course it is, since you aren't passing all required arguments. If you don't know what I'm talking about, you should consider taking a stepp back, look into learning how Java method calls and constructors work, and then come back and fix it :) I won't explain basic java or give you ready to use code.
     
  14. Offline

    KungFuGoat


    Well, i was sort of trying to learn as I go along :p
     
  15. Offline

    minoneer

    You should know the baiscs first. Once you do, that's perfectly fine.
     
  16. Offline

    KungFuGoat


    I figured it out :D Thanks for the help
     
  17. Offline

    minoneer

    Great! Good job :)
     
Thread Status:
Not open for further replies.

Share This Page