ArrayIndexOutOfBoundsException....

Discussion in 'Spigot Plugin Development' started by jusjus112, Jul 15, 2016.

  1. Dear forum members,

    I was trying to make a Queue system for being in a Queue to join minigame's.
    But, i seem to be stuck, because i always getting a ArrayIndexOutOfBoundsException
    everytime when i try to get "player_queue"

    Here is my code btw:

    Code (Text):

    public class UtilQueue {

        private int queueSize;
        private boolean isClosed;
       
        public UtilQueue(int queueSize, boolean isClosed){
          this.queueSize = queueSize;
          this.isClosed = isClosed;
        }
       
        private final HashMap<String,Integer> player_index = new HashMap<String,Integer>();
        private String[] player_queue = new String[queueSize];

        private int getNextSpot(){
          int freeIndex = 0;
          int i;
          for(i = 0; i <= queueSize; i++){
              if (player_queue.length > 0) {
                  if(player_queue[i] == null || player_queue[i] == ""){
                    freeIndex = i;
                    continue;
                  }
              }
          }
          return freeIndex;
        }
       
        public boolean isClosed() {
            return isClosed;
        }
       
        public void updateStatus(boolean status) {
            isClosed = status;
        }
       
        public boolean isInQueue(Player p) {
            if (player_index.containsKey(p.getName()) && player_index.get(p.getName())!=-1)
                return true;
            return false;
        }

        private boolean hasNextSpot(){
          if(getNextSpot() > queueSize) return false;
              else return true;
        }
         
        public boolean addToQueue(Player p){
            String pName = p.getName();
            if (isClosed) {
                p.sendMessage("§cDe Queue is op dit moment gesloten, kom later terug");
                return false;
            }
          if(hasNextSpot()){
              int nextSpot = getNextSpot();
              player_queue[nextSpot] = pName;
              player_index.put(pName, nextSpot);
              return true;
              }
          else return false;
        }
       
        public boolean removeFromQueue(String pName){
          if(hasNextSpot()){
              player_queue[player_index.get(pName)] = "";
              player_index.put(pName, -1);
              return true;
              }
          else return false;
        }

        public void addDefualtQueue(Player p){
          player_index.put(p.getName(), -1);
        }
       
        public String[] queue_select(int amountToSelect){
          String[] selected = new String[amountToSelect];
          int i;
          int index = 0;
          for(i = 0; i <= queueSize; i++){
             if(index > amountToSelect) continue;
                if(player_queue[i] != null && player_queue[i] != ""){
                   if(player_index.get(player_queue[i]) > 0){
                   selected[index] = player_queue[index];
                   removeFromQueue(player_queue[i]);
                   index++;
                   }
              }
          }
          return selected;
        }
    This is the class im using, i know that my String[] array is empty, but i dont know how to get back on track
     
  2. You should try using an ArrayList instead of a regular array this should make this 20x easier for you.
     
    • Agree Agree x 2
    • Like Like x 1
  3. Change to
    Code (Text):

          for(i = 0; i < queueSize; i++)[/CODE
    (Change from <= to <)
     
    • Agree Agree x 2
  4. Even better, use an actual Queue from Java lol