Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ExceptionInIntitializerError???

Status
Not open for further replies.

mackey333

Technical User
May 10, 2001
563
US
I get this exception on the indicated line when x is anything but 1...why?
Code:
    private void resizeArray(int x)
	{	
		int temp[][] = new int[x][2];
		
		for (int i=0; i<x; i++)
		{
		/*--->*/temp[i][0] = matches[i][0];
			temp[i][1] = matches[i][1];
		}
		
		matches = temp;
	}
-Greg :-Q

flaga.gif
 
Greg, if you want to copy the elements from &quot;matches&quot; into &quot;temp&quot; your for loop can only have an upper bound of the minium of &quot;x&quot; and &quot;matches.length&quot; yes? You have it hard coded to &quot;x&quot; therefore, depending on several variants, you are probably creating an &quot;out of bounds&quot; condition which triggers the exception.

good luck
-pete
 
What i am trying to do is this:

x is the number of elements that need to fit in the array.
If x = 4, then matches.length is 3 (always 1 less, method is called when another element needs to be added).

so,

1.)declare a new array that is 4x2
2.)the loop runs while i<x (0,1,2,3)
3.)point matches to temp

I don't understand where this exception is comming from. Also, it's not a standard ArrayIndexOutOfBounds exception to indicate a problem with the index values. Here is the entire class:
Code:
class MatchFinder
//finds consec nums and fills array matches[][]
{
	public int matches[][];
	public int target;
	
	private int counter;
	private int loopCount;
	private int start;
	private int total;
	
	MatchFinder(int t)
	{
		matches = new int[1][2];
		target = t;
		start = 1;
		loopCount = 0;
		counter = 0;
		doIt();
	}
	private void doIt()
	{
		do
		{
			for (int i=start; total<target; i++, loopCount++)
			{
				total += i;
				
				if (total == target)
				{
					++counter;
					resizeArray(counter);
					
					matches[counter - 1][0] = start;
					matches[counter - 1][1] = loopCount;
					
					//System.out.println(&quot;\n\n\n&quot; + matches[0][0] + &quot;    &quot; + matches[0][1]);
				}				
			}
			++start;
			total = 0;
			loopCount = 0;
			
		} while(start * 2 < target);
	}
	private void resizeArray(int x)
	{	
		if (x == 1){return;}
		
		int temp[][] = new int[x][2];
		
		for (int i=0; i<x; i++)
		{
			temp[i][0] = matches[i][0];
			temp[i][1] = matches[i][1];
		}
		
		matches = temp;
	}
}

To make it work, pass the constructor a positive integer. 6 will make the loop run once (work's fine), and 235 will make it go 3 times. -Greg :-Q

flaga.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top