0

I need to write the Java code for the Shunting Yard algorithm, I receive a string which I take to a queue and I start the evaluation. I got classes for Node, Stack, Queue which are supposed to work OK. My method balancear is this ...

public boolean balancear (Cola infijo) {

    balance=infijo;
    int balanceado=0;
    char parentesis;

    while (!balance.isEmpty()) {

        parentesis=balance.pop();
        if (parentesis=='(')
            balanceado++;
        else if (parentesis==')')
            balanceado--;
    }

    return balanceado==0;
}



package expresionpolaca;
public class Postfijo {

    Pila operadores = new Pila();
    Cola infijo = new Cola ();
    Cola postfijo = new Cola ();
    Herramientas evaluador;
    StringBuilder salida = new StringBuilder();
    int re;

    public Postfijo (String expresion) {

        for (int i = 0; i < expresion.length() ; i++) {
            infijo.push(expresion.charAt(i));

        }

    }

    public String Convertir () {


        evaluador.balancear(infijo);         // I get NullPointerException Right HERE

        if (evaluador.balancear(infijo)) {   // I get NullPointerException Right HERE

            while (!infijo.isEmpty()) {


                char tmp=infijo.pop();
            if (Character.isLetterOrDigit(tmp)) { 
                postfijo.push(tmp);
            }

            else  {
                switch (tmp) {

                case '(':
                    operadores.push(tmp);
                    break;
                case ')':

                    do {
                        tmp=operadores.pop();
                        if (tmp!='(') postfijo.push(tmp);
                    } while(tmp!='(');
                    operadores.pop();
                    break;

                default:
                    if (operadores.sneak()=='(') operadores.push(tmp);
                    else while (evaluador.prioridad(tmp)>evaluador.prioridad(operadores.sneak())||operadores.sneak()!='('||operadores.isEmpty()==false) {
                        postfijo.push(operadores.pop());
                    }
                    break;
                }

            }

        }

        while (!operadores.isEmpty()) postfijo.push(operadores.pop());

    }

    else return "No balanceada";

    while (!postfijo.isEmpty()) {

        salida.append(postfijo.pop());
    }

    return salida.toString();



}

}

gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
diegoaguilar
  • 8,179
  • 14
  • 80
  • 129

2 Answers2

2

You never initialize evaluador, you only declare it. Initialize it to an appropriate value before you use it.

Makoto
  • 104,088
  • 27
  • 192
  • 230
1

If evaluador is a global variable that you are not initializing in your method be sure that is initialized somewhere before using it

gtgaxiola
  • 9,241
  • 5
  • 42
  • 64