Error checking if fields are empty!

2

I'm trying to verify that the product registration fields are empty, probably the error is in the if statement, which was to be returning the error message:

  

"Fields not filled in, please try again"

With the running application, if I fill in all the fields of the product register it works perfectly, and it gets to the table in the database, but if I hit the register button with the empty fields it was for him to show me the error message, but instead the application 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();
                }
            }
        }
    });
}
}

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

  

"product.setValueCost (Double.parseDouble (edtVCusto.getText () .toString ())); (line 54)".

There are some parts of code commented on because I'm testing various forms.

    
asked by anonymous 08.11.2018 / 20:41

2 answers

0

The error you see has nothing to do with the save in the database, but rather with the setters you are calling, which are given 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 is empty, an exception is thrown, in the form of NumberFormatException . Notice that the very description of the exception tells you the problem:

  

java.lang.NumberFormatException: empty String

See that empty String or String empty is mentioned. When correcting this problem the two Integer.parseInt that comes down below will give you exactly the same problem.

See a simplified example of this exception in Ideone

To work around the problem you must verify that 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 having the user put blanks, you can use trim before equals :

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

You even already have if that verifies that all fields are filled but is not correct as it tests with null and is in the wrong place, after setters . Passing the Produto and its setters into the% test field% already works, but given the number of fields you are testing, you can also follow another path.

First construct a function to validate if all fields of an array of else are filled:

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

Then in% w / w of% when you want to validate, you name 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 you just call the setters and save in the database when you are sure that the fields are all populated.

    
09.11.2018 / 00:44
0

Take this whole stretch where you are setting the attributes of the product and place it inside the 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()){

   }
    
08.11.2018 / 22:34