sinat_33003361 2016-05-06 12:28 采纳率: 100%
浏览 1163
已采纳

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

直接代码

 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)
抛出这样的错误

_

  • 写回答

1条回答 默认 最新

  • xSeeker~ 2016-05-06 15:53
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作