java Lisp表达式计算器中的空指针异常

java Lisp表达式计算器中的空指针异常,java,Java,我编写这个程序是为了用Java计算Lisp表达式,但它在运行时给了我空指针异常,这是我的代码: import java.util.Queue; import java.util.LinkedList; import java.util.Stack; import java.util.ArrayList; class IterativeEvaluator2 { private ExpressionScanner expression; public IterativeEv

我编写这个程序是为了用Java计算Lisp表达式,但它在运行时给了我空指针异常,这是我的代码:

import java.util.Queue;
import java.util.LinkedList;
import java.util.Stack;
import java.util.ArrayList;


class IterativeEvaluator2
{ 
    private ExpressionScanner expression;

     public IterativeEvaluator2 (String expression)
    {
        this.expression = new ExpressionScanner(expression);
    }

    public double evaluate(Queue<Double> operandqueue)
    {
            // write your code here to create an explicit context stack
        Stack<Queue> temp_stack = new Stack<Queue>();

        char operator = ' ';
            double operand = 0.0;

            // write your code here to evaluate the LISP expression iteratively
            // you will need to use an explicit stack to push and pop context objects
        while ( expression.hasNextOperator() || expression.hasNextOperand() )
        {

            // Get the open bracket         

            if ( expression.hasNextOperator())    
                {
                operator = expression.nextOperator() ;
                if (operator == '(')
                { 
                    // if the stack is empty then the first bracket is trivial
                    // so this method will be instantiated by null
                    if (temp_stack.empty())
                        temp_stack.push(operandqueue);      
                    // else instantiate an arraylist(Queue)
                    else {
                        operandqueue = new LinkedList<Double>();
                        }
                }

                // push the list into the stack after the closing bracket appears       
                else if (operator == ')')
                {   
                    operand = calculate(operandqueue);
                    operandqueue = temp_stack.pop();
                    if (operandqueue != null)
                        operandqueue.offer(operand);

                }
            // if it is another operator then it must be +,-,/,*
                else {
                    operator = expression.nextOperator();
                    operandqueue.offer( (double) operator ); \\ this is where it says there is an error
                }
            }
            // else it is an operand so just put it in the queue
            else
                operandqueue.offer( (double) expression.nextOperand() ); 
        }
    return operand;
    }

        private double calculate(Queue<Double> some_queue)
        {
                char operator = (char) (some_queue.remove()).intValue();
                double operand1 = 0;
                double operand2;            
                switch(operator){
                    case '+' :  while( !some_queue.isEmpty() )
                            {
                                operand2 = some_queue.remove();  
                                operand1 = operand1 + operand2;         
                            }
                            break;

                    case '-' :  operand1 = some_queue.remove();
                            //checks for negative numbers
                            if (some_queue.isEmpty() )
                                operand1 = 0  -  operand1;
                            else{
                                while ( !some_queue.isEmpty() )
                                {
                                    operand2 = some_queue.remove();
                                    operand1 =  operand1 - operand2;
                                }
                            }
                            break;

                    case '*' :  operand1 = 1;
                            while ( !some_queue.isEmpty() )
                            {
                                operand2 = some_queue.remove();
                                operand1 = operand1*operand2;
                            }
                            break;

                    case '/' :  operand1 = some_queue.remove();
                            if (some_queue.isEmpty() )
                                                    operand1 = 1/operand1 ;
                                                else{
                                                        while (!some_queue.isEmpty())
                                {
                                        operand2 = some_queue.remove();
                                    operand1 = operand1/operand2;                                                                                           }
                            }
                            break;
                }
            return operand1;
        }

    public static void main(String [] args)
    {
        String s =  
        "(+\t(- 6)\n\t(/\t(+ 3)\n\t\t(- \t(+ 1 1)\n\t\t\t3\n\t\t\t1)\n\t\t(*))\n\t(* 2 3 4))";  // = 16.5
        IterativeEvaluator2 myEvaluator = new IterativeEvaluator2(s);
        System.out.println("Evaluating LISP Expression:\n" + s);
        System.out.println("Value is: " + myEvaluator.evaluate(null)); 
    }
}  /* 201340 */
你知道我做错了什么吗

谢谢, 卡尔


所以第58行是NPE,因为执行
operanqueue.offer()
operanqueue
null

请稍候。。。我在数。。。1、2、3、4……你需要告诉我们第58行是哪一行,给我们一个线索。顺便说一句:有趣的作业/任务。这通常是一个点,你也可以转储你的堆栈内容,以了解发生了什么。对不起,伙计们。第58行表示
操作数queue.offer((双)运算符)
Evaluating LISP Expression:
(+  (- 6)
    (/  (+ 3)
        (-  (+ 1 1)
            3
            1)
        (*))
    (* 2 3 4))
Exception in thread "main" java.lang.NullPointerException
    at IterativeEvaluator2.evaluate(IterativeEvaluator2.java:58)
    at IterativeEvaluator2.main(IterativeEvaluator2.java:122)
myEvaluator.evaluate(null)