Bigdecimal - Numberformatexception Error

Asked

Viewed 560 times

0

If an editText is not filled in the APK stops working and returns the error:

10-31 15:06:54.340 4152-4152/com.wms.www.calccred E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wms.www.calccred, PID: 4152
    java.lang.NumberFormatException: Bad offset/length: offset=0 len=0 in.length=0
        at java.math.BigDecimal.<init>(BigDecimal.java:276)
        at java.math.BigDecimal.<init>(BigDecimal.java:425)
        at com.wms.www.calccred.Consorcio$1.onClick(Consorcio.java:99)
        at android.view.View.performClick(View.java:6256)
        at android.view.View$PerformClick.run(View.java:24701)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

I don’t understand why scheduled messages should appear. Netbeans works smoothly without errors.

Follows the code:

public class Consorcio extends AppCompatActivity {

    private EditText edtCota;
    private EditText edtPrazo;
    private EditText edtReserva;
    private EditText edtAdm;
    private EditText edtVida;
    private Button bVoltar;
    private Button btnCalc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consorcio);


        edtCota = (EditText) findViewById(R.id.edtCota);
        edtPrazo = (EditText) findViewById(R.id.edtPrazo);
        edtReserva = (EditText) findViewById(R.id.edtReserva);
        edtAdm = (EditText) findViewById(R.id.edtAdm);
        edtVida = (EditText) findViewById(R.id.edtVida);
        bVoltar = (Button) findViewById(R.id.bVoltar);
        btnCalc = (Button) findViewById(R.id.btnCalc);


        //Evento Botao Calc
        btnCalc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Valores in = new Valores();


               /* BigDecimal cota = new BigDecimal(in.a);
                BigDecimal prazo = new BigDecimal(in.b);
                BigDecimal reserva = new BigDecimal(in.c);
                BigDecimal adm = new BigDecimal(in.d);
                BigDecimal vida = new BigDecimal(in.e);*/


                if (edtCota.getText().equals("")) {

                    AlertDialog.Builder janelaBrancoCota = new AlertDialog.Builder(Consorcio.this);
                    janelaBrancoCota.setMessage("Digite um valor para a Cota");
                    janelaBrancoCota.setNeutralButton("FECHAR", null);
                    janelaBrancoCota.show();

                } else if (edtPrazo.getText().equals("")) {

                    AlertDialog.Builder janelaBrancoPrazo = new AlertDialog.Builder(Consorcio.this);
                    janelaBrancoPrazo.setMessage("Digite um valor para o Prazo");
                    janelaBrancoPrazo.setNeutralButton("FECHAR", null);
                    janelaBrancoPrazo.show();


                } else if (edtReserva.getText().equals("")) {

                    AlertDialog.Builder janelaBrancoReserva = new AlertDialog.Builder(Consorcio.this);
                    janelaBrancoReserva.setMessage("Digite um valor para a Reserva");
                    janelaBrancoReserva.setNeutralButton("FECHAR", null);
                    janelaBrancoReserva.show();

                } else if (edtAdm.getText().equals("")) {

                    AlertDialog.Builder janelaBrancoAdm = new AlertDialog.Builder(Consorcio.this);
                    janelaBrancoAdm.setMessage("Digite um valor para a Taxa Administrativa");
                    janelaBrancoAdm.setNeutralButton("FECHAR", null);
                    janelaBrancoAdm.show();

                } else if (edtVida.getText().equals("")) {

                    AlertDialog.Builder janelaBrancoVida = new AlertDialog.Builder(Consorcio.this);
                    janelaBrancoVida.setMessage("Digite um valor para a Taxa Administrativa");
                    janelaBrancoVida.setNeutralButton("FECHAR", null);
                    janelaBrancoVida.show();

                } else {

                    String z1 = in.a = edtCota.getText().toString();
                    String z2 = in.b = edtPrazo.getText().toString();
                    String z3 = in.c = edtReserva.getText().toString();
                    String z4 = in.d = edtAdm.getText().toString();
                    String z5 = in.e = edtVida.getText().toString();

                    BigDecimal cota1 = new BigDecimal(z1);
                    BigDecimal prazo1 = new BigDecimal(z2);
                    BigDecimal reserva1 = new BigDecimal(z3);
                    BigDecimal adm1 = new BigDecimal(z4);
                    BigDecimal vida1 = new BigDecimal(z5);


                    //Calculo dos valores
                    if (cota1.compareTo(BigDecimal.ZERO) == 0) {

                        AlertDialog.Builder janelaZeroCota = new AlertDialog.Builder(Consorcio.this);
                        janelaZeroCota.setMessage("A Cota não pode ser 0");
                        janelaZeroCota.setNeutralButton("FECHAR", null);
                        janelaZeroCota.show();

                    } else if (prazo1.compareTo(BigDecimal.ZERO) == 0) {

                        AlertDialog.Builder janelaPrazo = new AlertDialog.Builder(Consorcio.this);
                        janelaPrazo.setMessage("O Prazo não pode ser 0");
                        janelaPrazo.setNeutralButton("FECHAR", null);
                        janelaPrazo.show();

                    } else if (reserva1.compareTo(BigDecimal.ZERO) == 0) {


                        AlertDialog.Builder janelaReserva = new AlertDialog.Builder(Consorcio.this);
                        janelaReserva.setMessage("A taxa Reserva não pode ser 0");
                        janelaReserva.setNeutralButton("FECHAR", null);
                        janelaReserva.show();

                    } else if (adm1.compareTo(BigDecimal.ZERO) == 0) {


                        AlertDialog.Builder janelaAdm = new AlertDialog.Builder(Consorcio.this);
                        janelaAdm.setMessage("A taxa Administrativa não pode ser 0");
                        janelaAdm.setNeutralButton("FECHAR", null);
                        janelaAdm.show();


                    } else if (vida1.compareTo(BigDecimal.ZERO) == 0) {


                        AlertDialog.Builder janelaVida = new AlertDialog.Builder(Consorcio.this);
                        janelaVida.setMessage("A taxa do Seguro de Vida não pode ser 0");
                        janelaVida.setNeutralButton("FECHAR", null);
                        janelaVida.show();


                    } else {

                        //Formato para exibição de valores
                        DecimalFormat decimal = new DecimalFormat();
                        decimal.applyPattern("R$" + "#,###.00");

                        //Calculo Fundo Comum
                        BigDecimal pc = new BigDecimal("100");
                        BigDecimal percentualMensal = pc.divide(prazo1, 4, RoundingMode.HALF_UP);
                        BigDecimal parcelaMensal = percentualMensal.multiply(cota1);
                        BigDecimal parcelaMensal1 = parcelaMensal.divide(pc);

                        //Calculo Taxa Administrativa
                        BigDecimal a1 = adm1.divide(prazo1, 4, RoundingMode.HALF_UP);
                        BigDecimal parcelaAdm = a1.multiply(cota1);
                        BigDecimal parcelaAdm1 = parcelaAdm.divide(pc);

                        //Calculo Fundo Reserva
                        BigDecimal r1 = reserva1.divide(prazo1, 4, RoundingMode.HALF_UP);
                        BigDecimal parcelaReserva = cota1.multiply(r1);
                        BigDecimal parcelaReserva1 = parcelaReserva.divide(pc);

                        //Calculo Seguro de Vida
                        BigDecimal v1 = vida1.divide(prazo1, 4, RoundingMode.HALF_UP);
                        BigDecimal parcelaVida = cota1.multiply(v1);
                        BigDecimal parceaVida1 = parcelaVida.divide(pc);

                        BigDecimal calculo = parcelaMensal1.add(parcelaAdm1.add(parcelaReserva1.add(parceaVida1)));


                        //Saida de informações para o usuario
                        AlertDialog.Builder janelaResultados = new AlertDialog.Builder(Consorcio.this);
                        janelaResultados.setMessage("Valor parcela :" + decimal.format(calculo));
                        janelaResultados.setNeutralButton("FECHAR", null);
                        janelaResultados.show();
                    }


                }
            }
        });

        //Evento botão voltar
        bVoltar.setOnClickListener(new View.OnClickListener()

        {
            public void onClick(View v) {
                Intent voltar = new Intent(Consorcio.this, Home.class);
                startActivity(voltar);
            }
        });

    }
}
  • The error also happens with all correctly filled Edttext?

  • No, if it pre-fills them works perfectly, until the part that checks if it was inserted 0. But if it leaves blank it hangs and returns this error

1 answer

2

The error indicates that you are trying to create a Bigdecimal with an invalid value, possibly a string empty.

The problem is how the test is being done in if’s.

Substitute

edtXXX.getText().equals("")

for

edtXXX.getText().toString().equals("")

Or better yet, pass this part of the code

String z1 = in.a = edtCota.getText().toString();
String z2 = in.b = edtPrazo.getText().toString();
String z3 = in.c = edtReserva.getText().toString();
String z4 = in.d = edtAdm.getText().toString();
String z5 = in.e = edtVida.getText().toString();

for before the first if and use

zX.equals("")

as a condition.

  • I tried the proposal and it still didn’t work, but thank you

Browser other questions tagged

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