• 06-23-2013, 07:23 AM
nu2programming
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, 07:23 AM
DarrylBurke
• 06-23-2013, 07:25 AM
nu2programming
I don't understand, what is that?
• 06-23-2013, 07:27 AM
DarrylBurke
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, 07:28 AM
DarrylBurke
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, 07:34 AM
nu2programming
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, 08:01 AM
nu2programming
• 06-23-2013, 08:26 AM
DarrylBurke
Quote:

Originally Posted by nu2programming

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

db
• 06-23-2013, 08:43 AM
nu2programming
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, 08:52 AM
DarrylBurke
On which line did you assign a value to console?

db
• 06-23-2013, 04:39 PM
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 :)
• 06-23-2013, 04:43 PM
DarrylBurke