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