Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: NullPointerException:null problem

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    10
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default NullPointerException:null problem

    Hello,

    I'm trying to make a simple calculator with Java and everything compiles well but when I try to run it, I get an error on the line
    String expression = keyboard.nextLine();
    saying "NullPointerException:null"

    I read a few webpages saying that it is trying to access something but that something is null? :S

    Thanks


    import java.util.Scanner;
     
    public class Calculator
    {
        private static Scanner keyboard;
     
        public static void main(String[] args)
        {
     
            System.out.println("Welcome to calculator 1.0");
            System.out.println();
     
            System.out.print("Enter an expression: ");
            String expression = keyboard.nextLine();
     
            // The user must enter an expression such as 2+3.
            // You can assume that only 1-digit numbers are typed so
            // that you can use expression.charAt(0) to get the first
            // number and so on.
     
            int number1; // the first number
            int number2; // the second number
            char operation; // the operation (+,-,*,/)
     
            number1 = expression.charAt(0);
            operation = expression.charAt(1);
            number2 = expression.charAt(2);
     
            // Now calculate the answer and print it out.
            //
            // Use an if/else statement to handle each operation
            int answer;
            if (operation == '+')
                answer = number1+number2;
            else if (operation == '-')
                answer = number1-number2;
            else if (operation == '*')
                answer = number1*number2;
            else if (operation == '/')
                answer = number1/number2;
     
                else
            {
                System.out.println("Unsupported operation: " + operation);
                return;
            }
     
            System.out.println("The answer is" +answer);
        }
    }


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: NullPointerException:null problem

    you forgot the correct instantiation of the object for the Scanner class



    you only have this
    ,
    private static Scanner keyboard;

    it should be like this,
    private static Scanner keyboard = new Scanner(System.in);

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: NullPointerException:null problem

    Indeed, you have a null reference, when you attempt to call a method on a null reference you will be thrown a NullPointerException.

        String myString = null;
        myString.length();

    // Json

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: NullPointerException:null problem

    sir json i notice his error and i corrected it.. but i need some brief explanation about the
    correct instantiation that i've posted

    can you please explain it how? and why?

    tnx

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: NullPointerException:null problem

    The dot operator references a method/field of an instantiated object. However, null means you're not pointing to an object, and can't reference a method/field of an object if it doesn't exist. So, the JVM is polite and throws a NullPointerException rather than crash your program/system

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: NullPointerException:null problem

    Here is a scary bit of code for you though.

    Try running this.

    public class MyClass {
     
        public static void myMethod() {
            System.out.println("No null pointer exception?");
        }
     
     
        public static void main(final String[] arguments) {
            MyClass myclass = null;
            myClass.myMethod();
        }
    }

    // Json

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: NullPointerException:null problem

    Eclipse is smart It won't let me compile.

    You're trying to access a static method via an instantiated object.

    However, if you using a different compiler, it's likely you won't get a null pointer exception (if it's smart, and thinks it knows better than you do). The reason is it may go through on compile and replace all attempts to call static methods un-statically, and replace them so they are called statically. It's impossible to get a null pointer exception from calling a method statically

  8. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: NullPointerException:null problem

    My eclipse compiles fine with that, you should be getting a compiler warning though.

    You can call statics using "this" as well if you like, during runtime the JVM will just reference the class rather than the instance anyways.

    // Json

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: NullPointerException:null problem

    I think I may have turned a setting on so it will give me an error. That's ok, it makes no sense to try and use a static member non-statically

Similar Threads

  1. [SOLVED] NullPointerException.CLARIFICATION
    By chronoz13 in forum Exceptions
    Replies: 8
    Last Post: August 28th, 2009, 03:24 PM
  2. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM
  3. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM
  4. [SOLVED] NullPointerException in Poker's program
    By dean_martin in forum Exceptions
    Replies: 10
    Last Post: April 21st, 2009, 06:40 AM
  5. NullPointerException in Java
    By jazz2k8 in forum Exceptions
    Replies: 9
    Last Post: June 1st, 2008, 05:59 PM