# Exception error on Main - Array Out of Bounds

Page 1 of 1

## 3 Replies - 235 Views - Last Post: 24 June 2015 - 09:51 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=377687&amp;s=a21237679a24cebdd11a1a1f2ccbb053&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 p0ep0e

Reputation: 0
• Posts: 2
• Joined: 20-June 15

# Exception error on Main - Array Out of Bounds

Posted 20 June 2015 - 01:02 PM

Hi! I am a Java beginner. I am doing the exercises in the Algorithms 4 text book for an online course. I am supposed to create a simple Fibonacci sequence using an array. I am getting the following error: "Exception in thread "main" - java.lang.ArrayIndexOutOfBoundsException: 0 - at fib.main(fib.java:9)". I've been working on making adjustments for more than an hour. I've tried watching videos, but I'm just not sure what the issue is. Any tips?! It'd be eternally grateful.

```public class fib {

public static void main(String args[]) {
//Declaring fibNum
int fibNum = 0;
//Creating array
int[] fibs =  new int[fibNum];
//Starting Points for Fibs
fibs[0] = 0;
fibs[1] = 1;
// System.out.println(F(100));
//Begin calculation
for(int N = 0; N < 100; N++){
fibs[N] = fibs[N - 1] + fibs[N - 2];
}
//System.out.println(fibNum);

for(int N = 0; N < 100; N++) {
System.out.print(fibs[N] + " ");
}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Exception error on Main - Array Out of Bounds

### #2 p0ep0e

Reputation: 0
• Posts: 2
• Joined: 20-June 15

## Re: Exception error on Main - Array Out of Bounds

Posted 20 June 2015 - 01:12 PM

Oh my goodness! I see what I did. I created a list from the bounds of zero! So I changed the code to what's below

```public class fib {

public static void main(String args[]) {
//Declaring fibNum
int fibNum = 100;
//Creating array
int[] fibs =  new int[fibNum];
//Starting Points for Fibs
fibs[0] = 0;
fibs[1] = 1;
// System.out.println(F(100));
//Begin calculation
for(int N = 0; N < 100; N++){
fibs[N] = fibs[N - 1] + fibs[N - 2];
}
//System.out.println(fibNum);

for(int N = 0; N < 100; N++) {
System.out.print(fibs[N] + " ");
}
}
}

```

Oh! And then I realized I could just use the declaration for fibNum rather than manually setting the values. So sorry. I think I got it now.

```public class fib {

public static void main(String args[]) {
//Declaring fibNum
int fibNum = 100;
//Creating array
int[] fibs =  new int[fibNum];
//Starting Points for Fibs
fibs[0] = 0;
fibs[1] = 1;
// System.out.println(F(100));
//Begin calculation
for(int N = 0; N < 100; N++){
fibs[N] = fibs[N - 1] + fibs[N - 2];
}
//System.out.println(fibNum);

for(int N = 0; N < 100; N++) {
System.out.print(fibs[N] + " ");
}
}
}

```

### #3 ndc85430

Reputation: 184
• Posts: 716
• Joined: 13-June 14

## Re: Exception error on Main - Array Out of Bounds

Posted 22 June 2015 - 05:18 AM

That still isn't quite correct, is it? Look at line 14: what happens when N is 0?

### #4 manisha mulchandani

Reputation: 1
• Posts: 3
• Joined: 24-June 15

## Re: Exception error on Main - Array Out of Bounds

Posted 24 June 2015 - 09:51 AM

I think you should start your loop from "2" because if you start it from 0 then at line 14

```   fibs[N] = fibs[N - 1] + fibs[N - 2];

```

So, For N=0
```fibs[0]=fibs[-1]+fibs[-2];
```

Set first two elements explicitly as follows:

```fibs[0]=0;
fibs[1]=1;
```

And then start your loop from N=2 for calculation of Fibonacci series.