Friday, October 30, 2009

Published 10/30/2009 by with 0 comment

java.lang.ArrayIndexOutOfBoundsException


It's a very common runtime error message or exception we face when we use array wrongly. Very specifically this exception is thrown to indicate that an array has been accessed with an illegal index (location / position / subscript). The index is either negative or greater than or equal to the size of the array.


See the following code:

int data[]={5,7,18,21,27,15,3};

//show the array elements
for(int i=0;i<=7;i++)
{
System.out.println(data[i]);
}

Here the array size is 7 as the array is initialized by assigning 7 elements {5,7,18,21,27,15,3}. So the valid array index is 0 to 6. In the loop condition, we have given i&lt=7 and i is used as the array index inside the loop - so when i is 7 it tries to access the array element 7 but 7 is not a valid index. This causes the exception. Correct loop condition is i<7 or i<=6. The recommended approach is using the array built in variable length instead of the static size. Correct the code as below:


int data[]={5,7,18,21,27,15,3};

//show the array elements
for(int i=0;i<data.length;i++)//data is the array name
{
System.out.println(data[i]);
}

Read More
    email this       edit
Published 10/30/2009 by with 0 comment

array dimension missing

'array dimension missing' is shown when the array size is not specified while allocating memory for the array or creating an array.

See below:

int data[ ]=new int[ ];

See that, blank [ ] is given in new int[ ]. We must give an int as the size of the array. Memory will be allocated for the array according to the given int within the [ ] as below:

int data[]=new int[5];
Read More
    email this       edit
Published 10/30/2009 by with 7 comments

illegal initializer

You may see this error for writing wrong expression to initialize an array. See the code below:

int data={42,15,27,20,19};

If you compile this, you will see the following error message:

illegal initializer for int
int data={42,15,27,20,19};
^

Here the array is not declared properly as we have not put [ ] for declaring the array. The correct code is:

int data[ ]={42,15,27,20,19};

or

int[ ] data={42,15,27,20,19};
Read More
    email this       edit
Published 10/30/2009 by with 1 comment

incompatible types

1. Sometimes we may get the following error message in an 'if' (selection) structure.

incompatible types
found : int
required: boolean

This is because, we have not specified a boolean expression as the condition of the 'if'; instead we might have written such an expression which is may be int, float, String or other types but not boolean.

See the code below:

int a=10,b=10;

if(a=b)
{
System.out.println("a and b are equal");
}

Check the condition is a=b. Is it a boolean expression? By using a single = (equal sign), value of b is assigned to a which is an integer. That's not correct. We must use == (double equal sign) for comparing two variables for equality.

Correct code is the below:

int a=10,b=10;

if(a==b)
{
System.out.println("a and b are equal");
}


2. Sometimes it happens because of assigning one type of value to a variable of other types. See the code below:

String text='a';
char ch="a";
In this case the error message will be:

incompatible types
found : char
required: java.lang.String
String text='a';
^
incompatible types
found : java.lang.String
required: char
char ch="a";
^
Here we have not assigned correct value for the variables text and ch. In the first line, we have assigned a char value to a String variable. 'a' is a char value because it is written within ' ' (single quotation mark) . In the second line, we have assigned String value "a" to a char type variable. Note that String value is written within " " (double quotation mark). So we tried to assign char value to a String variable and a String value to a char variable. The correct code is:

String text="a";
char ch='a';

If you see this error, check that the correct type of value or variable is assigned.

Read More
    email this       edit