NullPointerExcpetion [duplicate]

-4

Hello stackoverflow friends, I'm finishing my calculator and I'm having problems with nullpointer, I'm new to java and I'm not sure how to change the logic to solve the problem, there goes the code that I'm having problems

 public Double calcular() throws Exception 
{
int tamanhoFila = this.expressao.trim().length();
Fila fila = new Fila<>(tamanhoFila);
Pilha pilha = new Pilha<>(tamanhoFila);
String regex = "[0-9]+";
    String[] elementos = quebraExressao(this.expressao.trim());

    for (String token : elementos)
{
        if (!token.equals(""))
        {
    if (token.matches(regex))   // se for numero
    {
                fila.emfila(token);
    }
    else    // se for operador
    {
                if (pilha.isVazia())
                {
        pilha.insere(token);
                }
                else 
                {
        String ultimoOperador = pilha.getUltimoElemento().toString();

        if (Tabela.devoDesempilhar(ultimoOperador.charAt(0), token.charAt(0)))
        {
                        if (token.equals(")"))
                        {
            while(!pilha.getUltimoElemento().equals("("))
            {
                                fila.emfila(pilha.retira());
            }
                                pilha.retira(); // para retirar o (
                        }
                        else 
                        {
            while(Tabela.devoDesempilhar(ultimoOperador.charAt(0), token.charAt(0)))
            {
                                fila.emfila(pilha.retira());
                                if (pilha.isVazia())
                                {
                pilha.insere(token);
                break;
                                }
                                else 
                ultimoOperador = pilha.getUltimoElemento().toString();
            }
                        }
        }
        else 
        {
                        pilha.insere(token);
        }
                }

    }
        }
}

more precisely on this line

for (String token : elementos)

How to change the logic so that it solves the Null Pointer problem?

On request here is the method BreakingAttack:

private static String[] quebraExressao(String expressao)
{
    expressao = expressao.replaceAll(" ", "").replaceAll("", " ");
            String regex = "[0-9]";
            String[] elementos = expressao.split(" ");
            String[] elementos_retorno = new String[elementos.length];
            int j = 0;

            for (int i =0; i < elementos.length; i++)
            {
                if (elementos[i].matches(regex))
                {
                    if (i+1 < elementos.length)
                    {      
                      if (elementos[i+1].matches(regex))
                       {
                        elementos_retorno[j] = elementos[i] + elementos[i+1];
                        i++;
                       }
                        else 
                        {
                         elementos_retorno[j] = elementos[i];
                        }
                    }
                }
                else 
                {
                    elementos_retorno[j] = elementos[i];
                }

                j++;
            }

    return elementos_retorno;
}
    
asked by anonymous 18.05.2016 / 03:27

2 answers

0

Good evening,

Dude probably the error can be in this line here:

String[] elementos = quebraExressao(this.expressao.trim());

If the error is in, the elements must be null, check the method and parameter again of the line above that should be returning null.

    
18.05.2016 / 03:49
0

The problem you are having is why your function:

quebraExressao()

For some reason (which could not be seen, why the function is not in the code) is returning null.

When you run the foreach statements, it tries to pull an element from the elements list and assign it to the token variable you specified, and being the null array it can not call these internal pop functions from the array and throws it exception.

A solution to this would be you in the function of breakingExpression () put a "default" return, an empty Array, so that even in case your logic returns null, you would have a variable elements initialized.

 private String[] quebraExpressao() {
         String[] retorno = new String[]{};
         ....
         logica em que vc atribui o retorno esperado a variavel retorno
         e assim sobreescrevendo o "default" array vazio, caso ela não seja null
         ....
         return retorno;
 }
    
18.05.2016 / 04:00