Java:Null指针Deque迭代器

Java:Null指针Deque迭代器,java,exception,iterator,Java,Exception,Iterator,我被指派去写一个类,它用中缀符号表示一个数学表达式,然后用后缀符号将这个表达式转换成一个等价的表达式。那部分我已经完成了 我还被指派编写一个迭代器,让客户端代码在后缀表达式的标记上进行迭代 到目前为止,我的迭代器的代码是: class PostfixIterator implements Iterator<String> { //this is line 117 private Deque<String> postfix; public PostfixIterator

我被指派去写一个类,它用中缀符号表示一个数学表达式,然后用后缀符号将这个表达式转换成一个等价的表达式。那部分我已经完成了

我还被指派编写一个迭代器,让客户端代码在后缀表达式的标记上进行迭代

到目前为止,我的迭代器的代码是:

class PostfixIterator implements Iterator<String> { //this is line 117

private Deque<String> postfix;

public PostfixIterator(Deque<String> postfix) {
    this.postfix = postfix;
}

public boolean hasNext() {
    return postfix.isEmpty();
}

public String next() {
    return postfix.pop(); //this is line 130
}

}
根据我的编译器,return postfix.pop()的计算结果为null。我不知道为什么

那么,有没有人能帮我把这项工作做好,并解释一下为什么我现在拥有的东西不起作用

谢谢

以下是我的整个InfixToPost修复类:

import java.util.*;

public class InfixToPostfix{

    private Deque<String> postfix;

    public InfixToPostfix(String infix){

        Deque<String> postfix = new LinkedList<String>();
        Deque<String> infixQ = new LinkedList<String>();

        //tokenize the user input
        int i = 0;
        char ch;
        infix = infix.replaceAll("\\s","");//make sure there is no whitespace
         while(i < infix.length()){
             ch = infix.charAt(i);

             if(ch == '(' || ch == ')'|| ch == '+'|| ch == '-' 
                     || ch == '/' || ch == '%'|| ch == '*' 
                     || ch == '^'){
                 String s =ch+"";
                 infixQ.add(s);
                 i++;
             }
             else if (Character.isDigit(ch)){
                 String s ="";
                 int j = i;
                 char c = infix.charAt(j);
                 while(j <= infix.length()-1 && //accumulate the digits in that number
                       Character.isDigit(c = infix.charAt(j))){
                     s = s + c;
                     j++;
                 }
                 infixQ.add(s);
                 i=j;
             }
             else if (Character.isLetter(ch)){
                 String s ="";
                 int j = i;
                 char c = infix.charAt(j);
                 while(j <= infix.length()-1 && //accumulate the lettes in that variable
                       Character.isLetter(c = infix.charAt(j))){
                     s = s + c;
                     j++;
                 }
                 infixQ.add(s);
                 i=j;
             }
        }
        System.out.println(infixQ);

        //start shunting-yard
        Deque<String> stack = new ArrayDeque<String>();
        Iterator<String> itr = infixQ.iterator();
        while(itr.hasNext()){
            String s = itr.next();

            //if token is number or a variable, put it on the output queue
            if(Character.isDigit(s.charAt(0)) 
               || Character.isLetter(s.charAt(0))){
                postfix.add(s);
            }
           if(s.equals("(")){
                stack.push(s);
            }
            if(s.equals(")")){
                while((!stack.isEmpty())&&(!stack.peek().equals("("))){
                    postfix.add(stack.pop());
                }
                stack.pop();
            }
            if(s.equals("+") || s.equals("-")){
                while((!stack.isEmpty()) && (stack.peek().equals("+") 
                     || stack.peek().equals("-")
                     || stack.peek().equals("*") || stack.peek().equals("/")
                     || stack.peek().equals("^"))){
                     postfix.add(stack.pop());

                }
                stack.push(s);
            }
            if(s.equals("*") || s.equals("/") || s.equals("%")){
                if(!stack.isEmpty()){
                    while((!stack.isEmpty())&&(stack.peek().equals("*") 
                          || stack.peek().equals("/") 
                          || stack.peek().equals("%")
                          || stack.peek().equals("^"))){
                        postfix.add(stack.pop());            
                    }
                }
                stack.push(s);
            }
            if(s.equals("^")){
                stack.push(s);
            }       
        }
        while(!stack.isEmpty()){
            postfix.add(stack.pop());
        }
        System.out.println(stack.isEmpty());
        System.out.println(postfix);        
    }

    public Iterator<String> iterator(){
        return new PostfixIterator(postfix);
    }

    public static void main(String[] args){
        InfixToPostfix a = new InfixToPostfix("(123)^45+6*7/89");
        Iterator itr = a.iterator();
        System.out.println(itr.next()); // this is line 112

    }
    }

似乎您声明了两次变量“postfix”,应该只使用类变量“postfix”

公共类InfixToPostfix{
私有德克后缀;
公共InfixToPostfix(字符串中缀){
后缀=新的LinkedList();
//代码在这里
}
}

您的问题在于数据字段的范围
postfix

这就是你所拥有的:

public class InfixToPostfix{

    private Deque<String> postfix; <-- this is data field 

    public InfixToPostfix(String infix){

        Deque<String> postfix = new LinkedList<String>();
                         ^
                         |   
you declared that reference here again which shadows the data field. 
postfix is just visible in constructor and out of here your data field
is still pointing to null value.
可以写

  List<String> myList = new ArrayList< >();
                                      ^
                                      |
List myList=newarraylist<>();
^
|
若您可以在下面的代码中为迭代器函数选择不同的名称,那个就更好了,因为您可能会混淆读代码的人

public Iterator<String> iterator(){
        return new PostfixIterator(postfix);
    }
公共迭代器迭代器(){
返回新的后缀迭代器(后缀);
}

您的InfixToPostfix类在哪里?我不明白PostfixIterator将如何迭代InfixToPostfix?!!!您需要发布更多信息如果postfix.pop()返回null,那么它将被打印为null,因为“itr”本身为null,所以会发生异常。请验证这一点。你能发布你的InfixToPostfix类吗?a.迭代器();正确返回PostfixIterator类?现在需要发布堆栈跟踪plzPosted my stack trace。
public class InfixToPostfix{

    private Deque<String> postfix; <-- this is data field 

    public InfixToPostfix(String infix){

        Deque<String> postfix = new LinkedList<String>();
                         ^
                         |   
you declared that reference here again which shadows the data field. 
postfix is just visible in constructor and out of here your data field
is still pointing to null value.
postfix = new LinkedList<String>(); 
 List<String> myList = new ArrayList<String>();
  List<String> myList = new ArrayList< >();
                                      ^
                                      |
public Iterator<String> iterator(){
        return new PostfixIterator(postfix);
    }