Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8681243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:27:26+00:00 2026-06-12T21:27:26+00:00

Sorry if this is answered somewhere due to me missing something obvious, but I’ve

  • 0

Sorry if this is answered somewhere due to me missing something obvious, but I’ve been googling this for days now and it just doesn’t seem to make any sense. I’ve got 3 years of experience in Javascript and am getting into Java now, so I’m not behind on the basic concepts of anything and such.

I’m using IntelliJ for this, but it fails to point out the problem. The communication (access rights and instantiations) between my classes is fine, the code syntax and variable types are as well, etc, so I really can’t tell what it is.

I have a Data class, which just holds “read-only” data for the other classes to use.

public class Data {
    // snip
    public static int[][] specs = {
      {6,1,6,40},
      {5,2,5,30},
      {5,3,4,40},
      {4,4,3,60}
   };
}

There’s another class that has to read this data when it’s initialized.

public class Soldier {
    // snip
    public int range;
    public Soldier() {
        int x = ...; // user input
        range = Data.specs[x][1];
    }
}

The specs array itself contains its data as defined (ie the array is not empty), x is valid as an index of the specs array (ie 0 <= x <= 3), its type is int and Test has read access to the specs array (all confirmed with debug output statements). And yet, when it tries to set the value of range (then and only then, at that exact point), I get the “Index out of bounds” error.

Can someone please tell me what’s going wrong when trying to read the array? Or am I right in saying that this is really weird and I need to post the entire code?

Note: a small new test also shows that, if I change the code to first output a manually chosen value from the array and then set the value of range, the console prints the error statement (and exits the program) and follows it up by printing the manually picked value, but assigning the value and then asking to output range only throws the error… That makes absolutely no sense at all!

Edit: I’ve edited the code above. The class called Test is called Soldier in my code (I’m making a text-based game…). Below’s the stack trace, if it’s any good without the full code (which is way long). The basic structure of my program is this:

1) Boot contains the main method and instantiates a new Game

2) Game instantiates x Teams

3) each Team instantiates an Army

4) each Army instantiates x Soldiers

Each instance of the classes is set as an attribute of the instantiating class (public Army army; and an Army instantiation in the Team constructor, for example). It’s essentially a cascade of constructors instantiating subsequent classes and assigning them as their attributes.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Army.<init>(Army.java:13)
at Team.<init>(Team.java:19)
at Game.<init>(Game.java:22)
at Boot.main(Boot.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)5

Edit edit: here’s the semi-full code (I’m leaving out the stuff that has absolutely nothing to do with it, including the imports). It’s in no particular order and the classes are in separate .java files within the IntelliJ project. The game continues up to the point where a new Soldier asks for its type to be designated (the function performing the user input is working fine and validating the input as proven by a technically identical other part of the game).

public class Boot {
    public static void main(String[] args) {
        Object[] games = new Object[] {};
        if (Lib.userConfirmPrompt("Start the game?") == true) {
            do {
                games[games.length] = new Game();
            }
            while (Lib.userConfirmPrompt("Do you want to play again?") == true);
        }
        System.exit(0);
    }
}

public class Game {
    public Object[] teams = new Object[] {};
    public Game() {
        for (int i = 0;i < settings.xbots + 1;i++) {
            teams[teams.length] = new Team(this);
        }
    }
}

public class Team {
    public Game game;
    public Army army;

    public Team(Game p) {
        game = p;
        army = new Army(this);
    }
}

public class Army {
    public Team team;
    public static Object[] soldiers = new Object[] {};

    public Army(Team p) {
        team = p;
        for (int i = 0;i < team.game.settings.xsoldiers;i++) {
            soldiers[soldiers.length] = new Soldier(this);
        }
    }
}

public class Soldier {
    private Army army;
    public int sight;
    public int range;
    public int distance;
    public int damage;

    public Soldier(Army p) {
        army = p;
        int type = Lib.userTxtIntOptionsPrompt(Data.isoldiertypes);
        // HERE is where it crashes, type is assigned and valid but the array access fails
        sight = Data.isoldierspecs[type][0];
        range = Data.isoldierspecs[type][1];
        distance = Data.isoldierspecs[type][2];
        damage = Data.isoldierspecs[type][3];
    }
}

public class Data {
    public static List isoldiertypes = Arrays.asList("Scout","Private","Machinegunner","Grenadier");
    public static int[][] isoldierspecs = {
        {6,1,6,40},
        {5,2,5,30},
        {5,3,4,40},
        {4,4,3,60}
    };
}

public class Lib {
    private static Scanner input = new Scanner(System.in);

    // output
    // default: 1 query string to print
    public static void outBase(String query) {
        System.out.print(query);
    }

    public static void outStd(String query) {
        outBase(query + "\n");
    }
    // end of output

    // input
    // default: 1 query string to print,
    //          query and input are in-line (exception: userConfirmPrompt prints query block-wise and default instruction in-line before input),
    //          keeps user hostage until valid input is given (exception: userPrompt returns blindly)
    public static String userPrompt(String query) {
        outBase(query);
        return input.nextLine();
    }

    public static String userTxtPrompt(String query) {
        String menuinput = null;
        do {
            if (menuinput != null) {
                userHostage();
            }
            menuinput = userPrompt(query);
        } while (menuinput.length() == 0);
        return menuinput;
    }

    public static int userIntPrompt(String query) {
        String menuinput = null;
        do {
            if (menuinput != null) {
                userHostage();
            }
            menuinput = userTxtPrompt(query);
        } while(menuinput.matches("^-?\\d+$") == false);
        return new Integer(menuinput);
    }
    // end of input

    // options input
    // default: takes a List of options as argument,
    //          prints an enumerated list of these options string-wise,
    //          prompts for a numeral selection of the desired option and returns the number if valid
    public static int userTxtIntOptionsPrompt(List options) {
        int choice = 0;
        Boolean chosen = false;
        do {
            if (chosen == true) {
                userHostage();
            } else {
                chosen = true;
            }
            chosen = true;
            for (int i = 0;i < options.size() - 2;i++) {
                outStd((i + 1) + ") " + options.get(i) + ",");
            }
            outStd((options.size() - 1) + ") " + options.get(options.size() - 2) + "\nand " + options.size() + ") " + options.get(options.size() - 1) + ".");
            choice = userIntPrompt("Enter the number of the option you'd like to select: ") - 1;
        } while(choice < 0 || choice >= options.size());
        return choice;
    }
    // end of options input

    // miscellaneous
    public static void userHostage() {
        outStd("Invalid operation. Please try again.");
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:27:28+00:00Added an answer on June 12, 2026 at 9:27 pm

    The problem is in your Army class:

    public static Object[] soldiers = new Object[] {};
    

    You initialize an empty (length == 0) array named soldiers, but later you access:

    soldiers[soldiers.length] = new Soldier(this);
    

    This causes the failure.

    By definition, soldiers.length is out of the bound of the array (since the bound is from 0 to soldiers.length-1)


    To overcome it – make sure you allocate enough space in the array soldiers or use a dynamic array (ArrayList) instead. You can append elements to an ArrayList using ArrayList.add(), and you don’t need to know the expected size before filling it up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

sorry if this question has already been answered somewhere else, but I couldn't find
Sorry if this has been answered somewhere, but from what I found, no one
I'm sure this has been answered somewhere, already, but I just can't find it:
Sorry if this question is answered somewhere else but I tried searching several pages
Im very sorry if this has been answered before but I have been looking
Sorry if this has already been answered but I could not find it here.
Sorry if this has already been answered, but I can't find it if so.
Sorry if this is a silly question or if it's been answered already, but
Sorry if this has been answered somewhere; I'm not quite sure how to phrase
Sorry if this has been answered before, but I am trying to make an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.