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 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题