Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Unknown error in java driver

    Hello and I am having trouble with a small java app that I am writing. It is supposed to calculate multiplication and Fibonacci Numbers.
    I know I have the Fib Num working but n order for me to check it I need the driver to run as well and it is returning an error I don't know how to fix it.
    the code below....

    Java Code:
    public class FibonacciNumbers
    {
        public int calcFibonacci(int number)
        {
            if( number==0 || number == 1)
            {
                return number;
            }
            else
            {
                return calcFibonacci(number -1) + calcFibonacci(number - 2);
            }
            
            
        }
    }
    
    //The deal with this class is to show multiplication using recursive methods
    public class Multi
    {
        
        public int mult(int a, int b)
        {
            if(a == 1)
            {
                return a;
            }
            else
            {
                return mult(a-1 , b+b);
            }
        }
    }
    
    public class Driver
    {
        private static Scanner in;
        private static int n;
        private static int temp = 0;
        public static void main(String[] args)
        {
            
            System.out.println("Hello! Which calculator do you want to use? (Fib = 1/ Multi = 0");
            temp = in.nextInt();
            if(n == 1)
                {
                    FibonacciNumbers fnum = new FibonacciNumbers();
                    in = new Scanner(System.in);
                    int temp = 0;
            
                    System.out.println("You have chosen the Fibonacci Calculator.");
                    System.out.print("First off what number position do you want us to calculate the Fib Value of?  ");
                    temp = in.nextInt();
                    System.out.print("OK, You have entered the Fib Num position of " +temp);
                    System.out.println(" and it returned " +fnum.calcFibonacci(temp));
                }
            else
                {
                    Multi mult = new Multi();
                    in = new Scanner(System.in);
                    int a = 0;
                    int b = 0;
                
                    System.out.println("You have chosen Multiplication Calculator!!!");
                    System.out.print("First off please enter your first factor...  ");
                    a = in.nextInt();
                    System.out.println();
                    System.out.print("The second factor?");
                    b = in.nextInt();
                    System.out.print("OK, You have entered " +a +b);
                    System.out.println(" and the answer comes out to " +mult.mult(a, b));
                }
        }
    }
    Any help is greatly appreciated.
    Last edited by Dr_DerpyHooves; 11-30-2012 at 06:43 PM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Unknown error in java driver

    Hi,

    First this line:

    Java Code:
    temp = in.nextInt();
    Gave you NPE error because you haven't create a Scanner instance for the in variable.

    Next this line:

    Java Code:
    if(n == 1)
    If you read user selection and store it in temp variable you should check it using if (temp == 1).

    The other this is that you don't have to instantiate the scanner multiple times in your code as you did in the if and the else block.

Similar Threads

  1. Replies: 5
    Last Post: 03-13-2012, 01:51 PM
  2. Unknown error
    By tbrowne03 in forum New To Java
    Replies: 3
    Last Post: 11-27-2011, 03:43 AM
  3. pls. help on my unknown error
    By boybackup in forum AWT / Swing
    Replies: 4
    Last Post: 11-13-2011, 11:34 PM
  4. Replies: 0
    Last Post: 11-06-2011, 12:18 AM
  5. unknown error - really need help
    By smallmos1 in forum New To Java
    Replies: 2
    Last Post: 03-15-2011, 10:46 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
  •