在类调用期间获取空指针异常。可能的构造函数错误

时间:2016-05-06 19:08:57

标签: java constructor nullpointerexception stack

我正在编写一个程序,将中缀表达式作为一个字符串并对其进行评估,结果返回一个double。但是,当我从main调用Calculator类时,我得到一个空指针异常。我在构造函数中遗漏了什么吗?

这是我的主要课程,从中调用计算器:

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

}

这是我的计算器课程。

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

(我没有包含isOperand(),hasPrecedence()和popStackAndSolve()方法,因为它们可以正常工作,但是,如果你想看到它们,请告诉我。)

我相信我在这里犯了一个非常简单的PEBCAC错误,但我无法弄清楚它对我来说是什么。我花了几个小时浏览教科书,论坛和javadocs,无法找到解决方案。您可以提供任何建议。

编辑:它在行上抛出错误(double result = calc.solve(equation);)。

编辑2:解决了。非常感谢你。 id10t错误在工作。

3 个答案:

答案 0 :(得分:0)

您正在运行静态方法main(因为它是入口点),但是您的calc对象为空,直到创建Generator对象为止。您的注释行应该解决问题,因此请取消注释。

答案 1 :(得分:0)

由于main是静态的,因此永远不会调用Generator构造函数,并且永远不会为calc赋值。而不是在构造函数中初始化calc,而是在声明它时初始化它,即使用

public static Calculator calc = new Calculator();

而不是

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

答案 2 :(得分:0)

你的计算器是静态的,并且引用了null ..

调用Generator的构造函数可以修复

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