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

Printable View

• 06-23-2013, 08:23 AM
nu2programming
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);
}
}
• 06-23-2013, 08:23 AM
DarrylBurke
Re: I am trying to write the program for Fibonacci number in Java, please help!
• 06-23-2013, 08:25 AM
nu2programming
Re: I am trying to write the program for Fibonacci number in Java, please help!
I don't understand, what is that?
• 06-23-2013, 08:27 AM
DarrylBurke
Re: I am trying to write the program for Fibonacci number in Java, please help!
Quote:

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
• 06-23-2013, 08:28 AM
DarrylBurke
Re: I am trying to write the program for Fibonacci number in Java, please help!
Quote:

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
• 06-23-2013, 08:34 AM
nu2programming
Re: I am trying to write the program for Fibonacci number in Java, please help!
Lets see if this helps:
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);                         }         }```
• 06-23-2013, 09:01 AM
nu2programming
Re: I am trying to write the program for Fibonacci number in Java, please help!
line 21 is where the issue is...please help!
• 06-23-2013, 09:26 AM
DarrylBurke
Re: I am trying to write the program for Fibonacci number in Java, please help!
Quote:

Originally Posted by nu2programming
line 21 is where the issue is...please help!

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

db
• 06-23-2013, 09:43 AM
nu2programming
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.
• 06-23-2013, 09:52 AM
DarrylBurke
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
• 06-23-2013, 05:39 PM
kumarjivi
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 :)
• 06-23-2013, 05:43 PM
DarrylBurke
Re: I am trying to write the program for Fibonacci number in Java, please help!
Quote:

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