Java: index in array exists, ArrayIndexOutOfBoundsException: 0 - java

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.");
}
}

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.

The x should be greater than -1 and less than 4.
The stacktrace does not mention the Solder class, its in the conctructor of the Army class.
Any how, only knowing that the index should be within a range is not enough. As a programmer its your duty to validate the index before trying to access an element at that index.
if(index > 0 && index < array.length) {
//then only acess the element at index
Problem is the array soldiers is of size 0.

This line int x = ...; // user input implies that you are taking input in some fashion from the user and accessing the array with it. Are you checking this value to see that is in range (i.e., between 0 and 3)? If not, this may be why your testing works.
Edit: something like this might solve it for you:
public class Army {
public Team team;
public Vector<Soldier> soldiers;
public Army(Team p) {
soldiers = new Vector<Soldier>()
team = p;
for (int i = 0;i < team.game.settings.xsoldiers;i++) {
soldiers.add(new Soldier(this));
}
}
}
Judging by your other code, this sort of pattern will be useful in your Game object as well.

Related

Java - Generate random array with length given by input

I'm working on a school project of a Casino Java App using OOP and Swing for GUI.
I have to build a slot machine which receives a number of boxes by user input and when user hits the "play" button, It needs to retrieve a random combination of fruits with the length of the input.
For example, if user wants the slot machine to have 3 boxes, there should be a random combination of 3 fruits out of all the fruits I have in my array. Lets say I have 20 fruits available, I need to use only as many fruits as boxes. If user wants 5 boxes in the slot machine, I need to retrieve 5 fruit combination.
I hope i explained myself
For this project I created a Fruit enum class, and I mapped the name of the fruit with a picture of it to show it in the slot machine.
public enum Fruta {
PERA("pera.png", "pera"),
BANANA("banana.png", "banana"),
FRUTILLA("frutilla.png", "frutilla");
private String archivo;
private String descripcion;
Fruta(String archivo, String descripcion) {
this.archivo = archivo;
this.descripcion = descripcion;
}
public String getArchivo() {
return archivo;
}
public static Fruta obtenerPorValor(String text){
for (Fruta f : Fruta.values()) {
if (f.descripcion.equalsIgnoreCase(text)) {
return f;
}
}
return null;
}
}
In my controller class, 'Casino', I built the 'play' method:
public Fruta[] jugar(TicketView ticket,MaquinaTragamonedasView maquinaView) {
MaquinaTragamonedas maquina = buscarMaquina(maquinaView.getIdMaquina());
if(ticket.getCredito() >= maquina.getCosteJugada() && ticket != null) {
Fruta[] combinacion = maquina.jugar();
return combinacion;
}
return null;
}
Play method inside my MaquinaTragamonedas (slot machine) class:
public Fruta[] jugar () {
if(verificarRecaudacionMinima()){
mostrarMensaje();
};
return generarCombinacion(cantidadCasillas);
}
This is how i generate the random combination of fruits inside the Slot machine class:
public Fruta[] generarCombinacion(int cantidadCasillas) {
Random random = new Random();
Fruta[] combinacion = {Fruta.values()[random.nextInt(Fruta.values().length)], Fruta.values()[random.nextInt(Fruta.values().length)], Fruta.values()[random.nextInt(Fruta.values().length)]};
return combinacion;
}
As you can see I need the numer of boxes (cantidadCasillas) to retrieve the combination, but cant get my head to achieve this.
This code will take the parameter into account:
public Fruta[] generarCombinacion(int cantidadCasillas) {
Random random = new Random();
Fruta[] combinacion = new Fruta[cantidadCasillas];
for (int i = 0; i < cantidadCasillas; i++) {
combinacion[i] = Fruta.values()[random.nextInt(cantidadCasillas)];
}
return combinacion;
}
There are multiple ways in java to create an array. In your code you chose the way to directly instantiate the array with values, there you had no chance of making its size depend on the parameter.
new Fruta[cantidadCasillas] creates an array of the length of variable cantidadCasillas and the for-loop sets the values one-by-one.

Empty array in multi class project returning null after receiving data when called upon

I'm having an issue that has been giving me a an error saying java.lang.StackOverflowError: null. My program is sort of like a lottery, a random number is chosen, and based off the number, your item is chosen from a array. Now this works all handy and dandy, but when i try to insert the item received into an inventory. I get that error. Im pretty sure i set up my class wrong but i don't know how to construct an array in a separate class that receives data from another class, and returns the data back to the same class. Enough chit chatting, heres what my code looks like so far. (please dont mind the extra variables as this is a cropped portion of my code)
public class inventory {
private int inventorymain;
public String[] inventorymain() {
String[] inventorymain;
return inventorymain();
}
}
import java.util.Scanner;
import java.util.Random;
public class glue {
public static void main(String [] args) {
inventory inv = new inventory();
allskins a = new allskins();
Scanner s = new Scanner(System.in);
int selection = 0;
int invcount = -1;
Random rand = new Random();
do {
System.out.println(d.menue());
selection = s.nextInt();
if (selection == 1) {
invcount++;
int random = rand.nextInt(208);
System.out.println("You opend a: ");
System.out.println(a.allskins()[random]);
System.out.println("Your item has been put in your inventory, select inventroy from the menue to view all items.");
System.out.println("");
inv.inventorymain()[invcount] = (a.allskins()[random]);
}
}while (selection != 6);
}
}
Thank you
You're entering infinite recursive loop calling inventorymain()
public class inventory {
private int inventorymain;
public String[] inventorymain() {
String[] inventorymain;
return inventorymain(); // recursive call here without escape condition
}
}
It's better to keep the method name and variable name different to avoid such cases.

Fundamental misunderstanding of objects and attributes in Java

I'm sitting on an assignment for university and I'm at a point, where I fear I haven't really understood something fundamental in the concecpt of Java or OOP altogether. I'll try to make it as short as possible (maybe it's sufficient to just look at the 3rd code segment, but I just wanted to make sure, I included enough detail). I am to write a little employee management. One class within this project is the employeeManagement itself and this class should possess a method for sorting employees by first letter via bubblesort.
I have written 3 classes for this: The first one is "Employee", which contains a name and an ID (a running number) , getter and setter methods and one method for checking whether the first letter of one employee is smaller (lower in the alphabet) than the other. It looks like this:
static boolean isSmaller(Employee source, Employee target) {
char[] sourceArray = new char[source.name.length()];
char[] targetArray = new char[target.name.length()];
sourceArray = source.name.toCharArray();
targetArray = target.name.toCharArray();
if(sourceArray[0] < targetArray[0])
return true;
else
return false;
}
I tested it and it seems to work for my case. Now there's another class called EmployeeList and it manages the employees via an array of employees ("Employee" objects). The size of this array is determined via constructor. My code looks like this:
public class EmployeeList {
/*attributes*/
private int size;
private Employee[] employeeArray;
/* constructor */
public EmployeeList(int size) {
this.employeeArray = new Employee[size];
}
/* methods */
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
/* adds employee to end of the list. Returns false, if list is too small */
boolean add(Employee m) {
int id = m.getID();
if (id > employeeArray.length) {
return false;
} else {
employeeArray[id] = m;
return true;
}
}
/* returns employee at certain position */
Employee get(int index) {
return employeeArray[index];
}
/* Sets employee at certain position. Returns null, if position doesn't exist. Else returns old value. */
Employee set(int index, Employee m) {
if (employeeArray[index] == null) {
return null;
} else {
Employee before = employeeArray[index];
employeeArray[index] = m;
return before;
}
}
Now comes my real problem: In a third class called "employeeManagement" I am supposed to implement the sorting algorithm. The class looks like this:
public class EmployeeManagement {
private EmployeeList ml = new EmployeeList(3);
public boolean addEmployee(Employee e) {
return ml.add(e);
}
public void sortEmployee() {
System.out.println(ml.getSize()); // I wrote this for debugging, exactly here lies my problem
for (int n = ml.getSize(); n > 1; n--) {
for (int i = 0; i < n - 1; i++) {
if (Employee.isSmaller(ml.get(i), ml.get(i + 1)) == false) {
Employee old = ml.set(i, ml.get(i + 1));
ml.set(i+1, old);
}
}
}
}
The "println" before my comment returns "0" in console... I am expecting "3" as this is the size I gave the "EmployeeList" as parameter of the constructor within my "EmployeeManagement" class. Where is my mistake ? And how can I access the size of the object I created in the "EmployeeManagement" class (the "3") ? I'm really looking forward to your answers!
Thanks,
Phreneticus
You are not storing size in your constructor. Something like,
public EmployeeList(int size) {
this.employeeArray = new Employee[size];
this.size = size; // <-- add this.
}
Also, setSize isn't going to automatically copy (and grow) the array. You will need to copy the array, because Java arrays have a fixed length. Finally, you don't really need size here since employeeArray has a length.
The size variable you are calling is the class field. If you take a quick look at your code, the getter is getting the field (which is initialized as zero when created). The size you are using it. The good way of doing it would be to get the size of the array in the getter like this:
public int getSize() {
return employeeArray.length;
}
This would return the size of the array in the object.

Passing Enumerated argument

Im having an issue passing an enumerated value from my main to one of my classes. My program stores information about nerds: name, nerdfactor. It has a series of classes and subclasses. I have a super class which is extended to 2 other classes and a third class which extends to 1 of the before mentioned subclasses. what my program is supost to do is take filled arguments such as name, nerdfactor, and wether the nerd prefers WII, XBOX or PS3. Bellow is a clip from my main and the beginings of my subclass. If anyone can help me figure out how to pass the enumerated argument i'd be very grateful.
public static void main(String[] args) {
// List to keep a few nerds in
ArrayList<GenericNerd> nerdList = new ArrayList(10);
GenericNerd nerd = new GenericNerd("Bugger McNosePicker", 9);
nerdList.add(nerd);
nerd = new ConsoleGeek("Freddy where's my mommy", 9, WII); // the WII should be an enum value
nerdList.add(nerd);
}
Subclass ConsoleGeek
public class ConsoleGeek extends GenericNerd {
enum ConsoleType {
WII, PS3, XBOX
};
private ConsoleType console;
public void setConsoleType(ConsoleType console) {
this.console = console;
}
public ConsoleType getConsoleType(){
return console;
}
public ConsoleGeek(String name, int nerdFactor, ConsoleType console) {
super(name, nerdFactor);
if (name == null) {
System.out.println("Error: GenericNerd constructor - name is null!");
System.exit(1);
}
// the setNerdFactor method handles parameter range checking
setNerdFactor(nerdFactor);
this.name = name;
if (console == ConsoleType.WII) {
// make sure WII players nerd factor is less than 6!
if (nerdFactor > 5) {
System.out.println("Warning: " + name
+ " Prefers WII! (setting nerdFactor to 5).");
setNerdFactor(5);
}
// Make sure XBOX players nerd factor is between 3 and 8
} else if (console == ConsoleType.XBOX) {
if (nerdFactor < 3 || nerdFactor > 8) {
System.out.println("Warning: " + name
+ " Prefers XBOX! (setting nerdFactor to 8).");
setNerdFactor(8);
}
}
}
I thought about the idea of making a new type of nerd like
ConsoleGeek supergeek = new ConsoleGeek(arguments)
but I'm pretty sure its supposed to stay in the other format.
nerd = new ConsoleGeek("Freddy where's my mommy", 9, ConsoleGeek.ConsoleType.WII);

Java bug or feature?

Ok, here is the code and then the discussion follows:
public class FlatArrayList {
private static ArrayList<TestWrapperObject> probModel = new ArrayList<TestWrapperObject>();
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] currentRow = new int[10];
int counter = 0;
while (true) {
for (int i = 0; i < 10; i++) {
currentRow[i] = probModel.size();
}
TestWrapperObject currentWO = new TestWrapperObject(currentRow);
probModel.add(counter, currentWO);
TestWrapperObject testWO = probModel.get(counter);
// System.out.println(testWO);
counter++;
if (probModel.size() == 10) break;
}
// Output the whole ArrayList
for (TestWrapperObject wo:probModel) {
int [] currentTestRow = wo.getCurrentRow();
}
}
}
public class TestWrapperObject {
private int [] currentRow;
public void setCurrentRow(int [] currentRow) {
this.currentRow = currentRow;
}
public int [] getCurrentRow() {
return this.currentRow;
}
public TestWrapperObject(int [] currentRow) {
this.currentRow = currentRow;
}
}
What is the above code supposed to do? What I am trying to do is load an array as a member of some wrapper object (TestWrapperObject in our case). When I get out of the loop,
the probModel ArrayList has the number of elements it is supposed to have but all have the same value of the last element (an array of size 10 with each item equal to 9). This is not the case inside the loop. If you perform the same "experiment" with a primitive int value everything works fine. Am I missing something myself regarding arrays as object members? Or did I just encounter a Java bug? I am using Java 6.
You are only creating one instance of the currentRow array. Move that inside the row loop and it should behave more like you expect.
Specifically, the assignment in setCurrentRow does not create a copy of the object, but only assigns the reference. So each copy of your wrapper object will hold a reference to the same int[] array. Changing the values in that array will make the values appear to change for all other wrapper objects that hold a reference to the same instance of the array.
i don' t want to sound condescending, but always try to remember tip #26 from the excellent pragmatic programmer book
select isn't broken
it is very rare to find a java bug. keeping this in mind often helps me to look over my code again, turn it around, and shake out the loose bits until i finally discover where i was wrong. of course asking for help early enough is very encouraged, too :)

Categories

Resources