hi im making a class that has methods to calculate prime numbers, im having a problem modifying the getNthPrime method to call checkAndResize method which makes the array(sieve) expand if the primenumbers object has a smaller parameter than what the getNthPrime method's parameter is. How can i do this, I keep getting a fatal exception or an arrayOutofboundsexception(which makes sense).

Code:

package primes;
public class PrimeNumbers {
private boolean [] sieve;
public PrimeNumbers(int upper) {
initializeSieve(upper);
}
public int getNthPrime (int n){
int prime =0;
int position = -1;
while (prime < n )
{
position ++;
if (sieve[position])
prime++;
}
return position;
}
public int getNumberPrimeNumbers(int n){
int primes = 0;
for (int i =0 ; i < sieve.length ; i ++){
if (n > sieve.length){
checkAndResize(n);
initializeSieve(n);
if(sieve[i])
primes++;
}
else if (sieve[i])
primes++;
}
return primes;
}
public int getSieveSize ()
{
return sieve.length;
}
public boolean isPrime (int n) {
if (n > sieve.length){
checkAndResize(n);
initializeSieve(n);
}
return sieve[n];
}
// prints out the prime numbers inside sieve
public void printPrimeNumbers() {
int n = 0;
boolean first = true;
System.out.print("[");
for(int i = 0; i < sieve.length - 1; i++){
n++;
if(sieve[i] == true && n != sieve.length - 1) {
if(first) first = false;
else System.out.print(" ");
System.out.print(i);
}
}
System.out.print("]");
}
// checks length of sieve with N and then resizes sieve if nessecary.
private void checkAndResize (int n){
if ((n + 1) > sieve.length){
initializeSieve(2*n);
}
}
private void setMultiples (int k) {
for (int i = 2*k; i < sieve.length; i += k)
sieve [i] = false;
}
private void initializeSieve (int upper){
if ( upper < 2)
sieve = new boolean [2];
else
sieve = new boolean [upper + 1];
sieve[0] = false;
sieve[1] = false;
for (int i =2 ; i< sieve.length; i ++ )
sieve[i] = true;
int bound = (int) Math.ceil(Math.sqrt(sieve.length));
for (int i = 2 ; i < bound ; i ++)
if (sieve[i])
setMultiples (i);
}
private String booleanToString (boolean value)
{
if (value)
return "T";
else
return "F";
}
public String toString (){
StringBuffer buf = new StringBuffer("[");
for (int i = 0; i < sieve.length -1 ; i ++)
buf.append(booleanToString (sieve[i]) + " " );
buf.append(booleanToString (sieve[sieve.length -1]) + "]");
return buf.toString();
}
}

thanks in advance