Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0

    Unhappy 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);
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

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

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

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

    I don't understand, what is that?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

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

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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default 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. #7
    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0

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

    line 21 is where the issue is...please help!

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

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

    Quote Originally Posted by nu2programming View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default 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. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    kumarjivi is offline Member
    Join Date
    Jun 2013
    Location
    India
    Posts
    5
    Rep Power
    0

    Default 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. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

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

    Quote Originally Posted by kumarjivi View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Fibonacci Number Calculator
    By Dr_DerpyHooves in forum New To Java
    Replies: 2
    Last Post: 11-28-2012, 02:56 AM
  2. Replies: 7
    Last Post: 11-30-2011, 06:58 PM
  3. Generating Fibonacci Series with a Multithreaded Java Program
    By firesauce in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-20-2009, 07:26 AM
  4. A Fibonacci printing program
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 07:26 PM

Posting Permissions

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