# Array outOf Bound Problem

• 11-29-2011, 11:06 PM
amrmb09
Array outOf Bound Problem
hi,
when i instantiate an object (group) from the class Group and pass the object as a parameter to the method"GenerateRandomQues" implemented in another class, i receive an error says: Null pointer Exception
To Note: Group [][]group = new Group[INDEX][LEVEL];// index=8 and level=2

can u please tell me where my problem is..

Code:

```public static void main (String[] args)  throws FileNotFoundException, IOException           {                                          Group [][]group = new Group[INDEX][LEVEL]; //[#of Question(index)] [level]```
Code:

```public class GenerateRandomQues {                 private String [][]packet= new String [5][];             private String randomisedQues[][];             public String [][] randomise(Group [][]group, int level)         {                 packet[3] = new String[4];                 Random rand = new Random();                 int randIndex = rand.nextInt(3);                                 this.packet[0][0] = group[randIndex][level].getGroupIndex();                 this.packet[1][0] = group[randIndex][level].getGroupDiff();                 this.packet[2][0] = group[randIndex][level].getGroupQues();                                 this.packet[3][0] = group[randIndex][level].getGroupOptions()[0];                 this.packet[3][1] = group[randIndex][level].getGroupOptions()[1];                 this.packet[3][2] = group[randIndex][level].getGroupOptions()[2];                 this.packet[3][3] = group[randIndex][level].getGroupOptions()[3];// this method returns array of options options[]                                 this.packet[4][0] = group[randIndex][level].getGroupAnswer();                 return this.packet;         }```
• 11-29-2011, 11:21 PM
pbrockway2
Re: Array outOf Bound Problem
Quote:

array out of bound;
Your code will throw an ArrayIndexOutOfBounds exception if you try and access an array using an invalid index value. By "invalid" I mean either negative or greater than or equal to the length of the array. A simple example:

Code:

```Thing foo[] = new Thing[5]; System.out.println(foo.length); // prints 5 foo[0] = new Thing(); // ok foo[5] = new Thing(); // bad! the valid indexes only go up to 4 foo[-1] = new Thing(); // bad!```
Notice how the length property of an array can be used to print out and check what the length is.

Double arrays can be more tricky. An expression like

Code:

`arr[a][b] = ...`
only makes sense if both:

(1) a is less than arr.length

and

(2) b is less than arr[a].length.

(Further, arr[a] must not be null.) In your code you should check that arrays have the length you are assuming they do.
• 11-29-2011, 11:37 PM
amrmb09
Re: Array outOf Bound Problem
i have test the "packet" array concerning number of rows and columns using
packet.length=5 // ok
packet[0].length=2 //
packet[3].length // generate Null Pinter Exception
• 11-30-2011, 12:02 AM
pbrockway2
Re: Array outOf Bound Problem
Quote:

packet[3].length // generate Null Pinter Exception
This is telling you that packet[3] has the value null. That is, you have not assigned a value to packet[3].

Perhaps you could post the code that results in this error.
• 11-30-2011, 12:03 AM
amrmb09
Re: Array outOf Bound Problem
the code is mentioned above
• 11-30-2011, 12:19 AM
pbrockway2
Re: Array outOf Bound Problem
I'm sorry, but I can't really tell what's going on. The original post was edited to alter the compiler message, the code it contains is not runnable and does not contain the expression packet[3].length.

As I said if packet[3].length throws a NullPointerException but packet.length does not, that's because packet[3] is null.

Runnable code with the exact and entire runtime message is really required to say any more.
• 11-30-2011, 02:02 AM
amrmb09
Re: Array outOf Bound Problem
thanks alot...i solved, but storing my data in 1D array instead of 2D array
Regards.....