Results 1 to 7 of 7
Thread: Array outOf Bound Problem
- 11-29-2011, 10:06 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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 10:11 PM.
- 11-29-2011, 10:21 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Array outOf Bound Problem
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:array out of bound;
Notice how the length property of an array can be used to print out and check what the length is.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!
Double arrays can be more tricky. An expression like
only makes sense if both:Java Code:arr[a][b] = ...
(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, 10:37 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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-29-2011, 11:02 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Array outOf Bound Problem
This is telling you that packet[3] has the value null. That is, you have not assigned a value to packet[3].packet[3].length // generate Null Pinter Exception
Perhaps you could post the code that results in this error.
- 11-29-2011, 11:03 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
Re: Array outOf Bound Problem
the code is mentioned above
- 11-29-2011, 11:19 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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, 01:02 AM #7
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
Similar Threads
-
Array bound exception
By lakshmibvaraprasad in forum New To JavaReplies: 3Last Post: 07-19-2011, 02:02 PM -
array out of bound exception
By farahm in forum New To JavaReplies: 6Last Post: 12-19-2010, 09:10 PM -
Array out of bound- Recursive Method
By hpayandah in forum New To JavaReplies: 2Last Post: 11-12-2010, 08:02 PM -
Array index Out of bound Exception
By nitin_daviet88 in forum New To JavaReplies: 9Last Post: 07-28-2010, 05:32 AM -
Array Index Out of bound exception
By abhijit in forum NetworkingReplies: 7Last Post: 09-25-2009, 07:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks