Go Back   Java Programming Forums > Java Standard Edition Programming Help > Exceptions


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 17-09-2009, 05:32 AM
Junior Member
 

Join Date: Sep 2009
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
derky is on a distinguished road
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


Java Code
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);
    }
}



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 17-09-2009, 06:39 AM
chronoz13's Avatar
Java kindergarten
 

Join Date: Mar 2009
Location: Squatters Mansion
Posts: 405
Thanks: 113
Thanked 21 Times in 21 Posts
chronoz13 is on a distinguished road

I'm feeling Stressed
Default Re: NullPointerException:null problem

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



you only have this
,
Java Code
private static Scanner keyboard;
it should be like this,
Java Code
private static Scanner keyboard = new Scanner(System.in);
Reply With Quote
  #3 (permalink)  
Old 17-09-2009, 12:09 PM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,157
Thanks: 54
Thanked 136 Times in 132 Posts
Json will become famous soon enoughJson will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
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.

Java Code
    String myString = null;
    myString.length();
// Json
Reply With Quote
  #4 (permalink)  
Old 17-09-2009, 03:44 PM
chronoz13's Avatar
Java kindergarten
 

Join Date: Mar 2009
Location: Squatters Mansion
Posts: 405
Thanks: 113
Thanked 21 Times in 21 Posts
chronoz13 is on a distinguished road

I'm feeling Stressed
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
Reply With Quote
  #5 (permalink)  
Old 18-09-2009, 01:28 AM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
  #6 (permalink)  
Old 18-09-2009, 07:21 AM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,157
Thanks: 54
Thanked 136 Times in 132 Posts
Json will become famous soon enoughJson will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
Default Re: NullPointerException:null problem

Here is a scary bit of code for you though.

Try running this.

Java Code
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
Reply With Quote
  #7 (permalink)  
Old 18-09-2009, 03:27 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
  #8 (permalink)  
Old 18-09-2009, 04:12 PM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,157
Thanks: 54
Thanked 136 Times in 132 Posts
Json will become famous soon enoughJson will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
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
Reply With Quote
  #9 (permalink)  
Old 18-09-2009, 08:06 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] NullPointerException.CLARIFICATION chronoz13 Exceptions 8 28-08-2009 08:24 PM
Request.getParameter() value becoming null when i use entype="multipart/form-data" radhikavk JavaServer Pages: JSP & JSTL 4 13-08-2009 10:54 AM
Null Pointer Exception runtime error RoadRunner Exceptions 1 26-04-2009 06:21 PM
[SOLVED] crazy NullPointerException dean_martin Exceptions 10 21-04-2009 11:40 AM
[SOLVED] NullPointerException jazz2k8 Exceptions 9 01-06-2008 10:59 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 02:00 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.