-1

I am writing a program to take in infix expressions as a string and evaluate them, returning a double as a result. However, when I call the Calculator class from main, I get a null pointer exception. Am I missing something in my Constructor?

Here is my main class where the calculator is called from:

import java.util.Scanner;
public class Generator {

public static Calculator calc;

public Generator() {
    calc = new Calculator();
}

public static void main(String [] args) {
    Scanner scan = new Scanner(System.in);
    //calc = new Calculator();
    System.out.println("Enter the string you want to process");
    String equation = scan.nextLine();
    double result = calc.solve(equation);

    System.out.println("Result is: " + result);
}

}

And here is my Calculator class.

public class Calculator {

public Stack<Double> operands;
public Stack<Character> operators;

//Constructor
public Calculator() {
    operands = new Stack<>();
    operators = new Stack<>();
}

public Double solve(String equation) {
char current;
String number1 = "";
double value1;

for(int i = 0; i < equation.length(); i++) {
    current = equation.charAt(i);

    switch(current) {
        case '1': case'2': case'3':
        case '4': case'5': case'6':
        case '7': case'8': case'9': case '0': 
            while(isOperand(current))
                number1 += current;
            value1 = Double.parseDouble(number1);
            operands.push(value1);
            break;

        case '+': case '-': case '*': case '/':
           while(!operators.empty() && !hasPrecedence(operators.peek(), current)) {
               value1 = popStackAndSolve(operands, operators);
               operands.push(value1);
           }
           operators.push(current);
           break;
        case '(':
            operators.push(current);
            break;
        case ')':
            char topOperator = operators.peek();
            while(topOperator != '(') {
                value1 = popStackAndSolve(operands, operators);
                operands.push(value1);
                topOperator = operators.peek();
            }
            operators.pop();
            break;
        default: 
            break;
    }
    while(!operators.empty()) {
        value1 = popStackAndSolve(operands, operators);
        operands.push(value1);
    }
}   

return operands.peek();
}

(I have not included the isOperand(), hasPrecedence(), and popStackAndSolve() methods because they work as they should, however, if you would like to see them anyway, please let me know.)

I believe I am making a very simple PEBCAC error here, but I can't figure out what it is for the life of me. I have spent hours going through textbooks, forums, and javadocs and can't find the solution. Any advice you could offer is appreciated.

EDIT: It is throwing the error on the line (double result = calc.solve(equation);).

EDIT 2: Solved. Thank you very much. id10t error at work.

3 Answers3

0

You're running the static method main (because its the entry point) but your calc object is null until a Generator object is made. Your commented line should fix the problem, so un-comment it.

Zircon
  • 4,677
  • 15
  • 32
0

Because main is static, the Generator constructor is never called, and calc is never assigned a value. Instead of initializing calc in the constructor, initialize it when it's declared, i.e. with

public static Calculator calc = new Calculator();

instead of

public static Calculator calc;
public Generator() {
    calc = new Calculator();
}
amiller27
  • 491
  • 5
  • 8
0

your calculator is static and null referenced..

calling the constructor of the Generator can fix that

public static void main(String [] args) {
    Scanner scan = new Scanner(System.in);
    Generator  gen = new Generator ();
    System.out.println("Enter the string you want to process");
    String equation = scan.nextLine();
    double result = calc.solve(equation);

    System.out.println("Result is: " + result);
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97