Nullpointerexcpetion

Asked

Viewed 61 times

-4

Hello stackoverflow friends, I’m finishing my calculator and I’m having nullpointer problems, I’m new in java and do not know how to change the logic to solve the problem, there goes the code 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 that line

for (String token : elementos)

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

On request here is the method Breakexpression:

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;
}

2 answers

0

Good night,

Guy probably the mistake might be on this line here:

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

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

0

The problem you are having is why your function:

quebraExressao()

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

When running the foreach instructions she tries to boot an element from the list elements and assigns it to the token variable that you specified, and since the null array cannot call these internal functions pop from the array and ae plays the exception.

A solution for this would be you in the break functionExpressao() put a return "default", 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;
 }
  • If I put the code can give a better view?

  • Yes , puts the breakExpressao

  • I will edit the post

  • I added the method

  • That . replaceAll is just to take out the blanks ? If it’s the first replaceAll already does that, you wouldn’t need the second

  • Unfortunately it was not I who did this part , but I was told that it is not just for this =/

  • Well, since I don’t know if you really need the second replaceAll, in my view when the code arrives in that split, it wouldn’t split , because it wouldn’t find the white space that you ask it to use as a delimiter. In this case , I think you should debug this function , would put a breakpoint at the beginning of the function and would see the values assigned to the variables to see if they are within the expected. Your problem is in function logic, the rest is debugging and finding the error.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.