2 sinat 33003361 sinat_33003361 于 2016.05.06 20:28 提问

我在做后缀表达式时,遇到空指针问题,有高手看下吗

直接代码

 package work;

public class Fun {
    public boolean isOperator(char c){
        if('+'==c||'-'==c||'*'==c||'/'==c||'^'==c||'%'==c){
            return true;
        }else
            return false;
    }

    public boolean isOpenParenthesis(char c){
        return '('==c;
    }

    public boolean isCloseParenthesis(char c){
        return ')'==c;
    }

    public int priority(char c){
        if(c=='^')return 3;
        if('*'==c||'/'==c||'%'==c)return 2;
        else if ('+'==c||'-'==c)return 1;
        else return 0;
    }

    public String converToPostfix(String expression)throws Exception{
        LinkList st = new LinkList();
        String postfix = new String();
        for(int i = 0;i<expression.length()||expression!=null;i++){
            char c = expression.charAt(i);
            System.out.println(postfix);
            if(' '!=c){
                if(isOpenParenthesis(c)){
                    st.insert(c);
                }
                else if(isCloseParenthesis(c)){
                    char ac = (Character)st.pop();
                    while(!isOpenParenthesis(c)){
                        postfix = postfix.concat(String.valueOf(ac));
                        ac = (Character)st.pop();
                    }
                }
                else if(isOperator(c)){
                    if(!st.isEmpty()){
                        char ac = (Character)st.pop();
                        while(ac != 0 && priority(ac)>=priority(c)){
                            postfix = postfix.concat(String.valueOf(ac));
                            ac = (Character)st.pop();
                        }
                        if(ac != 0){
                            st.insert(ac);
                        }               
                    }       
                    st.insert(c);
                }
                else{
                    postfix = postfix.concat(String.valueOf(c));
                }
            }
        }
            while(st.isEmpty())
                postfix = postfix.concat(String.valueOf(st.pop()));
            return postfix;
    }

}
 package work;


public class LinkList {
    public Node head ;

    public void insert(Object date){
        Node p = new Node(date);
        p.next = head;
        head = p;
    }
    public Object pop(){
        if(isEmpty())
            return null;
        else{
            Node p = head;
            head = head.next;
            return p.date;
        }
    }

    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return head==null;
    }
}
 package work;

public class Node {
    public Object date;
    public Node next;
    //�޲�
    public Node(){
        this.next = null;
    }
    //�в�
    public Node(Object date){
        this();
        this.date = date;

    }
}
 package work;

public class Example {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Fun f = new Fun();
        String postfix = f.converToPostfix("1+2+3");
        System.out.println(postfix);
    }

}

Exception in thread "main" java.lang.NullPointerException
at work.Fun.converToPostfix(Fun.java:48)
at work.Example.main(Example.java:8)
抛出这样的错误

_

2个回答

bealing
bealing   Rxr 2016.05.06 23:53
已采纳

at work.Fun.converToPostfix(Fun.java:48) ac = (Character)st.pop(); 如果st为空,那么你就不能再pop了,所以要在while的循环条件里面加上 !st.isEmpty()

CSDNXIAON
CSDNXIAON   2016.05.06 20:32

在做Android分享时遇到的一些问题
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!