Multi-Dimensional Array and ArrayIndexOutOfBoundsException - java

I have a strange problem which I can't fix:
A field:
private boolean[][][] gaps;
Constructor (1st line):
gaps = new boolean[NOBARRICADES][WIDTH][HEIGHT];
Constructor (2nd line):
for (int i = 0; i < NOBARRICADES; i++) {
Java throws an error for the 2nd line, saying:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
Does it have anything to do with Java syntax (the mistake is in these lines of code) or I should look for the problem somewhere else?

You're probably misreading the error output. Your second line does not even access the array - make sure that it's not the first line of the body of the for-loop that throws the exception. Also, make sure that you use i only to index the first dimension of your array.

sometimes the java compiler is off by a line or two. You may check the lines of code around the line that it says the error is on and see if you see anything.

Sorry, but you really don't want to do that.
Multidimensional arrays are never worth the confusion they cause--they have no positive value at all (with the POSSIBLE exception of a clear, obvious x,y array).
I suggest you try starting with either a list of two-dimensional arrays or a two-dimensional array of objects where each object contains a list.

Related

Unable to compare two values within the 2D array

I am working on a problem in which I have to compare two values within the 2D array but I am unable to get the output. Kindly help me to come out of this problem. Here's a glimpse of my code:
int arr[][]=new int[N][N];
for(j=0;j<arr.length;j++)
{
for(k=0;k<arr[j].length;k++)
{
if(arr[j][k]==arr[j][k+1])
c++;
}
}
The problem is that you will get an ArrayOutOfBoundsException due to the comparison if(arr[j][k]==arr[j][k+1]). This happens when you reach the last element of the array since when k is strictly less than arr[j].length, then k+1 element doesn't exist.
Maybe you want to change the condition to k<arr[j].length-1. But I am not sure what you actually what to achieve with the code.

Nesting string.splits in a for loop - inner split gets NPE when assigned

So, today I hit a snag trying to split a string array element which has been split from a long string already.. It works like this:
A string is created from a file. This string is split at blank lines (\n\n) which are basically paragraphs of text, which gets used as body text elsewhere.
Each paragraph begins with a topic, followed by an asterisk, followed by the body text.
Here's the issue -
It would be too simple to, while iterating through each paragraph string element to split these at the asterisk character (escaped, of course). To demonstrate, the first part works well:
sections = formatted.split("\n\n"); //sections previously declared as string array
int lines = sections.length ;
for(int i= 0 ; i< lines; i++) {
sections[i] = "Heading\n\n" + sections[i] ;
Now, when trying to perform another task in each iteration (yes, just join the two code windows together in your mind), it throws NPE on the second resulting index:
String paragraph = sections[i];
String[] half = paragraph.split("\\*");
topics[i] = half[0]; //Topics also previously declared as array
}
The last line inside the iterator throws a NPE (not out of bounds). I can't tell if it's the I or the ) index.
I would really appreciate understanding why this doesn't work. Perhaps it's because I always seem to hit these problems well after midnight...
How can I build these two parallel instance arrays? Thanks!
It seems likely to me that Topics is null - there's nothing within the code you've shown to assign it a non-null value. You probably want:
Topics = new String[lines];
before the loop. You should be able to spot this pretty easily in the debugger, too. (If you're confused about where an NPE is coming from, adding diagnostics and/or debugging are usually the first steps.)
Also note that conventionally your variables would be called sections and topics, as variables in Java are usually camelCased.

Java - java.lang.ArrayIndexOutOfBoundsException

I'm trying to run my code and it says something about the exception:
java.lang.ArrayIndexOutOfBoundsException
I googled it, from what I understand it happens when I try to access an index that's negative or greater than my array length.
But I can't seem to find the problem, here's my code: http://pastebin.com/sXsBbYfh
Thanks for any helpers.
EDIT: the error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Calculator.addOne(Calculator.java:127)
at Calculator.add(Calculator.java:88)
at Program.main(Program.java:8)
About the relevant part of the code, I have no idea, that's why i'm coming to you.
The issue would appear to be with line 86
arrResult = this.addOne(arrResult.length);
Array indexes are 0 based, so 0 - length-1 and you are passing length in and then using that to access your array on line 127
switch(arrResult[arrayIndex])
This part of code :
public int[] addOne(int arrayIndex)
124.
{
125.
switch(arrResult[arrayIndex])
126.
{
127.
case 0:
128.
arrResult[arrayIndex] = 1;
Is source of error.
Note that.
In java, arrays index range from 0 to length-1
In your code above, when method addon() is called , you are passing array length as parameter, and in code above you are trying to access array[length] which does not exist and hence the exception. Thus you may want to keep that length-1
In the following code line #86
arrResult = this.addOne(arrResult.length);
There are lot of logical errors in your code. This is just the one that throws the exception you mentioned

Value (not index) greater than 3 in array causes java.lang.ArrayIndexOutOfBoundsException

I'm a novice programmer. This may be a simple problem but I've never seen this before. First of all, let me clarify that I'm not even trying to manipulate the index. Here's the part of the code that is causing the exception:
int[] bumpercatcher = new int[4];
//time variable that helps control events
int time = 0;
public void setup()
{
bumpercatcher[0]=4;
bumpercatcher[1]=4;
bumpercatcher[2]=4;
bumpercatcher[3]=4;
As you can see I'm trying to set them all equal to 4 at the start of the program. This causes the arrayindexoutofbounds exception. If I set them all equal to 0~3 then there is no problem (until I set them to a value greater than 3 later in the program). I don't understand it.
-it doesn't matter if I set the array size to 10, I still get the same exception
-it doesn't matter if I set only one of the values (i.e. at index 1, which is definitely within bounds of the array). same exception
Is there something I'm doing wrong? Thanks.
well, here' the entire code if you want to take a look(not too long, 1 class, bad programming practies): http://dl.dropbox.com/u/33501308/Pong.java
Here's the html from which you can see the program from (not much to see. it just freezes instantly.): http://dl.dropbox.com/u/33501308/bin.zip
by the way I'm using eclipse.
I don't really know what SSCEE is. sorry
Your posted code file includes loops along the lines of
for(int j: bumpercatcher) {
if(bumpercatcher[j]>5)
...
}
This is an issue. This is a different kind of loop than a traditional for loop. It is an extended or enhanced for, also called a foreach. It reads "for each integer j in array bumpercatcher do x." You are taking your element j (a value) and using it as an index to the array. When your value exceeds the maximum index, you will get an exception.
Write your code with a proper for loop if you want to access by index, or try simply restructuring your logic like
for (int j : bumpercatcher) {
if (j > 5) // j is the value!
...
}
I'm not sure what the problem is, but a far more readable way of doing this would be with a for loop:
for(int i = 0; i < bumpercatcher.length; i++) {
bumpercatcher[i] = 4;
}

what EXACTLY does index 0 requested with a size of 0 means?

I'm getting this error in my application. I just want to know what does this error exaclty means. It would be really good if you give a simple example to explain
it just means your Array or Cursor or what ever is not having any elements, its length is 0. so
assume
String[] s = {};//not sure if it compiles. just an example for understanding
s[0]//doing this means asking for 1st elemnt when s does't have any..
You are asking for the first element of an empty array. There is no first element, so you get an exception.
Without seeing your code (which would be very helpful here), it sounds like you've got an empty array or list, and you're asking for the first element (which doesn't exist). For example:
String[] anArray = {"Hello", "world"}; //An array with two elements
System.out.println(anArray[0]); //prints "Hello". anArray[0] gets the first element.
String[] anotherArray = {}; //An empty array. There's nothing here to get.
System.out.println(anotherArray[0]); //throws an exception because there's no first element to get.
I think the same thing probably happens with ArrayList and other container classes, but I don't remember offhand. I hope that's EXACTLY what you were looking for.

Categories