Results 1 to 7 of 7
  1. #1
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default 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..

    Java Code:
    public static void main (String[] args)  throws FileNotFoundException, IOException
    	  {   			    
    	    Group [][]group = new Group[INDEX][LEVEL]; //[#of Question(index)] [level]
    Java 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;
    	}
    Last edited by amrmb09; 11-29-2011 at 11:11 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: Array outOf Bound Problem

    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:

    Java 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

    Java 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.

  3. #3
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default 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

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: Array outOf Bound Problem

    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.

  5. #5
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Array outOf Bound Problem

    the code is mentioned above

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default 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.

  7. #7
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Array outOf Bound Problem

    thanks alot...i solved, but storing my data in 1D array instead of 2D array
    Regards.....

Similar Threads

  1. Array bound exception
    By lakshmibvaraprasad in forum New To Java
    Replies: 3
    Last Post: 07-19-2011, 03:02 PM
  2. array out of bound exception
    By farahm in forum New To Java
    Replies: 6
    Last Post: 12-19-2010, 10:10 PM
  3. Array out of bound- Recursive Method
    By hpayandah in forum New To Java
    Replies: 2
    Last Post: 11-12-2010, 09:02 PM
  4. Array index Out of bound Exception
    By nitin_daviet88 in forum New To Java
    Replies: 9
    Last Post: 07-28-2010, 06:32 AM
  5. Array Index Out of bound exception
    By abhijit in forum Networking
    Replies: 7
    Last Post: 09-25-2009, 08:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •