Error checking that fields are empty!

Asked

Viewed 472 times

2

I’m trying to check if the product registration fields are empty, probably the error is in the declaration of if, that was to is returning the error message:

"Unfilled fields, try again"

With the application running, if I fill in all fields of the product registration it works perfectly, and reaches the table in the bank, but if I press the sign up button with the empty fields it was for him to show me the error message, but instead, the app hangs and restarts.

Process: com.example.rodrigoconceicao.controleestoque2_1, PID: 2818
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:539)
at com.example.rodrigoconceicao.controleestoque2_1.CadastroProduto$1.onClick (CadastroProduto.java:54)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
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)
2018-11-08 16:13:46.384 492-1756/system_process E/EGL_emulation: tid 1756: eglSurfaceAttrib(1210): error 0x3009 (EGL_BAD_MATCH)
2018-11-08 16:13:56.703 492-509/system_process E/memtrack: Couldn't load memtrack module
2018-11-08 16:16:00.004 492-509/system_process E/memtrack: Couldn't load memtrack module
2018-11-08 16:18:00.006 492-509/system_process E/memtrack: Couldn't load memtrack module
2018-11-08 16:18:46.615 492-1165/system_process E/ActivityManager: Found activity ActivityRecord{4229246 u0 com.example.rodrigoconceicao.controleestoque2_1/.TelaLogin t-1 f} in proc activity list using null instead of expected ProcessRecord{60cfefe 
2818:com.example.rodrigoconceicao.controleestoque2_1/u0a68}
2018-11-08 16:18:46.643 492-563/system_process E/InputDispatcher: channel '512a963 com.example.rodrigoconceicao.controleestoque2_1/com.example.rodrigoconceicao.controleestoque2_1.CadastroProduto (server)' ~ Channel is unrecoverably broken and will be disposed!
2018-11-08 16:18:46.643 492-563/system_process E/InputDispatcher: channel 'fad8f39 com.example.rodrigoconceicao.controleestoque2_1/com.example.rodrigoconceicao.controleestoque2_1.TelaPrincipal (server)' ~ Channel is unrecoverably broken and will be disposed!
2018-11-08 16:18:46.643 492-563/system_process E/InputDispatcher: channel 'fb3b8a9 com.example.rodrigoconceicao.controleestoque2_1/com.example.rodrigoconceicao.controleestoque2_1.Produtos (server)' ~ Channel is unrecoverably broken and will be disposed!
2018-11-08 16:18:46.643 492-563/system_process E/InputDispatcher: channel '324fdd0 com.example.rodrigoconceicao.controleestoque2_1/com.example.rodrigoconceicao.controleestoque2_1.TelaLogin (server)' ~ Channel is unrecoverably broken and will be disposed!
2018-11-08 16:18:46.833 1168-1315/com.android.launcher3 E/EGL_emulation: tid 1315: eglSurfaceAttrib(1210): error 0x3009 (EGL_BAD_MATCH)
2018-11-08 16:19:00.004 492-509/system_process E/memtrack: Couldn't load memtrack module

This is the error message!

public class CadastroProduto extends AppCompatActivity {

Spinner spnFornecedor, spnUMedida;
EditText edtDescricao, edtCategoria, edtVCusto, edtVVenda, edtEAtual, edtEMinimo;
Button btnNFornecedor, btnCadastrar02;

DBHelper db;

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

    db = new DBHelper(this);

    spnFornecedor = (Spinner) findViewById(R.id.spnFornecedor);
    spnUMedida = (Spinner) findViewById(R.id.spnUMedida);

    edtDescricao = (EditText) findViewById(R.id.edtDescricao);
    edtCategoria = (EditText) findViewById(R.id.edtCategoria);
    edtVCusto = (EditText) findViewById(R.id.edtVCusto);
    edtVVenda = (EditText) findViewById(R.id.edtVVenda);
    edtEAtual = (EditText) findViewById(R.id.edtEAtual);
    edtEMinimo = (EditText) findViewById(R.id.edtEMinimo);

    btnNFornecedor = (Button) findViewById(R.id.btnNFornecedor);
    btnCadastrar02 = (Button) findViewById(R.id.btnCadastrar02);

    ArrayAdapter adapterUM = ArrayAdapter.createFromResource (this,R.array.unidade_medida, android.R.layout.simple_spinner_item);
    spnUMedida.setAdapter(adapterUM);

    btnCadastrar02.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Produto produto = new Produto();

            //String fornecedor = spnFornecedor.getSelectedItem().toString();
            produto.setUnidadeMedida(spnUMedida.getSelectedItem().toString());
            produto.setDescricao(edtDescricao.getText().toString());
            produto.setCategoria(edtCategoria.getText().toString());
            produto.setValorCusto (Double.parseDouble(edtVCusto.getText().toString()));
            produto.setValorVenda(Double.parseDouble(edtVVenda.getText().toString()));
            produto.setEstoqueAtual(Integer.parseInt(edtEAtual.getText().toString()));
            produto.setEstoqueMinimo(Integer.parseInt(edtEMinimo.getText().toString()));
            //String unidadeMedida = spnUMedida.getSelectedItem().toString();
            /*String descricao = edtDescricao.getText().toString();
            String categoria = edtCategoria.getText().toString();
            String valorCusto = edtVCusto.getText().toString();
            String valorVenda = edtVVenda.getText().toString();
            String estoqueAtual = edtEAtual.getText().toString();
            String estoqueMinimo = edtEMinimo.getText().toString();*/

            if (edtDescricao.getText().toString().equals(null) ||
                    edtCategoria.getText().toString().equals(null) ||
                    edtVCusto.getText().toString().equals(null) ||
                    edtVVenda.getText().toString().equals(null) ||
                    edtEAtual.getText().toString().equals(null) ||
                    edtEMinimo.getText().toString().equals(null)) {
                Toast.makeText(CadastroProduto.this, "Campos não preenchidos, tente novamente", Toast.LENGTH_SHORT).show();
            }

            else {

                long res = db.CriarProduto(produto);
                if (res>0){
                    Toast.makeText(CadastroProduto.this, "Produto cadastrado", Toast.LENGTH_SHORT).show();

                    Intent i = new Intent (CadastroProduto.this, Produtos.class);
                    startActivity(i);

                }else {
                    Toast.makeText(CadastroProduto.this, "Cadastro invalido, tente novamente", Toast.LENGTH_SHORT).show();
                }
            }
        }
    });
}
}

This is where the message indicates the error, but right on the line that says

"SetValorCusto (Double.parseDouble(edtVCusto.gettext(). toString()); (line 54)".

There are some parts of code commented because I am testing various forms.

2 answers

1

The mistake you see has nothing to do with saving the bank, but with the setters who is calling, who receive the value in Double, these:

produto.setValorCusto (Double.parseDouble(edtVCusto.getText().toString()));          
produto.setValorVenda(Double.parseDouble(edtVVenda.getText().toString()));

If the String passed to parseDouble an exception, in the form of NumberFormatException. Note that the exception description itself tells you the problem:

java.lang.Numberformatexception: Empty String

See that it is mentioned empty String that is to say String empty. When fix this problem both Integer.parseInt that comes just below will give exactly the same problem.

See a simplified example of this exception in Ideone

To get around the problem you have to check whether the String is not empty before converting. A simple way is:

if (edtVCusto.getText().toString().equals("")){
    produto.setValorCusto (Double.parseDouble(edtVCusto.getText().toString()));
}

If you want to avoid the case of the user putting blanks, you can use the trim before the equals:

if (edtVCusto.getText().toString().trim().equals("")){
    produto.setValorCusto (Double.parseDouble(edtVCusto.getText().toString()));
}

You already have the if which checks that all fields are filled in but is also not correct as it tests only with null and is in the wrong place after the setters. Passing the creation of the Produto and their respective setters into the else field testing already works, but given the amount of fields you are testing you can also follow another path.

First builds a function to validate whether all fields an array of EditText are filled:

private boolean camposPreenchidos(EditText[] campos){
    for (EditText campo: campos){
        if (campo.getText().toString().trim().equals("")){
            return false;
        }
    }
    return true;
}

Then in the onCreate when you want to validate, you call it:

@Override
protected void onCreate(Bundle savedInstanceState) {
    //...

    btnCadastrar02.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        EditText[] campos = {edtDescricao, edtCategoria, edtVCusto, edtVVenda, edtEAtual, edtEMinimo};
        if (!camposPreenchidos(campos)){
            Toast.makeText(CadastroProduto.this, "Campos não preenchidos, tente novamente", Toast.LENGTH_SHORT).show();
        }
        else {
            //Os seus setters e salvar no banco aqui
        }
    });
}

Notice that only calls the setters and keeps it on the bench when he’s sure the fields are all filled.

0

Take this whole chunk where you’re setting the product attributes and put it into Else. The error occurs when trying to convert an empty field to Double, since it has no value in Editext no if check if fields are empty with

   if(edtDescricao.getText().toString().isEmpty()){

   }

Browser other questions tagged

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