# Thread: I am trying to write the program for Fibonacci number in Java, please help!

1. Member
Join Date
Jun 2013
Posts
7
Rep Power
0

## I am trying to write the program for Fibonacci number in Java, please help!

I wrote the code for the Fibonacci number, and I do not have any errors but when I run the program in Eclipse it is saying Exception in thread "main" java.lang.NullPointerException
at testinclass.main(testinclass.java:20)
Since I am a new Java student I don't understand what that means, here is the code so far:

import java.io.Console;
import java.util.Scanner;

public class FibonacciNumber {

private static Scanner console;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int previous1;
int previous2;
int current = 0;
int counter;
int nthFibonacci;

System.out.println("Enter the first Fibonacci number: ");
int previous11 = console.nextInt();

System.out.println("Enter the second Fibonacci number: ");
int previous21 = console.nextInt();

System.out.println("The first two numbers of the Fibonacci sequence are: "
+ previous11 + "and" + previous21);

System.out.println("Enter the position of the desired number in " +
"the Fibonacci sequence: ");
int nthFibonacci1 = console.nextInt();

if (nthFibonacci1 == 1)
current = previous11;
else if (nthFibonacci1 == 2)
current = previous21;
else
{
counter = 3;

while (counter <= nthFibonacci1)
{
current = previous21 + previous11;
previous11 = previous21;
previous21 = current;
counter++;
}
}

System.out.println("\nThe " + nthFibonacci1 + "th Fibonacci number of "
+ "the sequence is: " + current);
}
}

3. Member
Join Date
Jun 2013
Posts
7
Rep Power
0

## Re: I am trying to write the program for Fibonacci number in Java, please help!

I don't understand, what is that?

4. ## Re: I am trying to write the program for Fibonacci number in Java, please help!

when I run the program in Eclipse it is saying Exception in thread "main" java.lang.NullPointerException
at testinclass.main(testinclass.java:20)
Some variable on line 20 hasn't been assigned a value. It's easy to guess -- just look at line 20.

When posting code on a forum, please do the readers the courtesy of removing inane IDE-generated comments which have absolutely no relevance to your problem.

db

5. ## Re: I am trying to write the program for Fibonacci number in Java, please help!

Originally Posted by nu2programming
I don't understand, what is that?
Try taking more than two minutes to read two linked pages. Then maybe you'll understand.

db

6. Member
Join Date
Jun 2013
Posts
7
Rep Power
0

## Re: I am trying to write the program for Fibonacci number in Java, please help!

Lets see if this helps:
Java Code:
```import java.util.Scanner;

public class FibonacciNumber {

private static Scanner console;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//Variables
int previous1;
int previous2;
int current = 0;
int counter;
int nthFibonacci;

//Prompt the user for input
System.out.println("Enter the first Fibonacci number: ");
previous1 = console.nextInt();

System.out.println("Enter the second Fibonacci number: ");
previous2 = console.nextInt();

System.out.println("The first two numbers of the Fibonacci sequence are: "
+ previous1 + "and" + previous2);

System.out.println("Enter the position of the desired number in " +
"the Fibonacci sequence: ");
nthFibonacci = console.nextInt();

if (nthFibonacci == 1)
current = previous1;
else if (nthFibonacci == 2)
current = previous2;
else
{
counter = 3;

while (counter <= nthFibonacci)
{
current = previous2 + previous1;
previous1 = previous2;
previous2 = current;
counter++;
}
}

System.out.println("\nThe " + nthFibonacci + "th Fibonacci number of "
+ "the sequence is: " + current);
}
}```

7. Member
Join Date
Jun 2013
Posts
7
Rep Power
0

8. ## Re: I am trying to write the program for Fibonacci number in Java, please help!

Originally Posted by nu2programming
I already told you... which variable on that line hasn't been assigned a value?

db

9. Member
Join Date
Jun 2013
Posts
7
Rep Power
0

## Re: I am trying to write the program for Fibonacci number in Java, please help!

I understand but when I put int previous1 = console.nextInt();
my console gives me an error...I'm thinking about rewriting the code again, sometimes that works.

10. ## Re: I am trying to write the program for Fibonacci number in Java, please help!

On which line did you assign a value to console?

db

11. Member
Join Date
Jun 2013
Location
India
Posts
5
Rep Power
0

## Re: I am trying to write the program for Fibonacci number in Java, please help!

You are encountering this exception as you have not initialized private static Scanner console
Just initialize the console reference variable (like private static Scanner console = new Scanner(System.in);) and it will work fine.
Thank you :)

12. ## Re: I am trying to write the program for Fibonacci number in Java, please help!

Originally Posted by kumarjivi
You are encountering this exception as you have not initialized private static Scanner console
Just initialize the console reference variable (like private static Scanner console = new Scanner(System.in);) and it will work fine.
Thank you :)
It's nice that you want to help, but please don't spoonfeed answers as that denies a learning opportunity to the member asking for help.

db

#### Posting Permissions

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