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
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);
}
}
Re: NullPointerException:null problem
you forgot the correct instantiation of the object for the Scanner class
you only have this
,
Code :
private static Scanner keyboard;
it should be like this,
Code :
private static Scanner keyboard = new Scanner(System.in);
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.
Code :
String myString = null;
myString.length();
// Json
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 \m/
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 :)
Re: NullPointerException:null problem
Here is a scary bit of code for you though.
Try running this.
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
Re: NullPointerException:null problem
Eclipse is smart :P 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 :)
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
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 :)