Error saving data from an sqlite registry

3

I'm having trouble saving data from a people record in sqlite JavaNullPointerException: invalid int: (51) 3452-2470. This error started to occur after I applied a mask. Here are the codes below.

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

public abstract class Mask {
public static String unmask(String s) {
    return s.replaceAll("[.]", "").replaceAll("[-]", "")
    .replaceAll("[/]", "").replaceAll("[(]", "")
    .replaceAll("[)]", "");
}

public static TextWatcher insert(final String mask, final EditText ediTxt) {
    return new TextWatcher() {
        boolean isUpdating;
        String old = "";
        public void onTextChanged(CharSequence s, int start, int before,int   count) {
            String str = Mask.unmask(s.toString());
            String mascara = "";
            if (isUpdating) {
                old = str;
                isUpdating = false;
                return;
            }
            int i = 0;
            for (char m : mask.toCharArray()) {
                if (m != '#' && str.length() > old.length()) {
                    mascara += m;
                    continue;
                }
                try {
                    mascara += str.charAt(i);
                } catch (Exception e) {
                    break;
                }
                i++;
            }
            isUpdating = true;
            ediTxt.setText(mascara);
            ediTxt.setSelection(mascara.length());
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void afterTextChanged(Editable s) {}
    };
}
}  

The next code is my sign-up screen.

 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import br.gestaoBd.BancoDeDados.ClienteDao;
 import br.gestaoBd.Beans.Cliente;

 public class CadCliente extends Activity {

EditText edId, edNome, edEmail, edTelefone, edEndereco, edCpf, edRg;
ClienteDao cliDao;

@Override
public void onCreate(Bundle icicle) {

    super.onCreate(icicle);
    setContentView(R.layout.cad_clientes);
    edId = (EditText) findViewById(R.id.cadEdId);
    edNome = (EditText) findViewById(R.id.cadEdNome);
    edEmail = (EditText) findViewById(R.id.cadEdEmail);
    edTelefone = (EditText) findViewById(R.id.cadEdTelefone);
    edTelefone.addTextChangedListener(Mask.insert("(##)####-####", edTelefone));
    edEndereco = (EditText) findViewById(R.id.cadEdEndereco);
    edCpf = (EditText) findViewById(R.id.cadEdCpf);
    edCpf.addTextChangedListener(Mask.insert("###.###.###-##", edCpf));
    edRg = (EditText) findViewById(R.id.cadEdRg);
    edRg.addTextChangedListener(Mask.insert("##.###.###-#", edRg));

    Cliente clienteRecebido = (Cliente) getIntent().getSerializableExtra("Cliente");
    if (clienteRecebido != null) {
        montaTela(clienteRecebido);
    } else {
        montaTela(new Cliente());
    }

    Button btnSalvar = (Button) findViewById(R.id.btSalvar);
    btnSalvar.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Cliente cli = new Cliente();
            cli.setId(Integer.valueOf(edId.getText().toString()));
            cli.setNome(edNome.getText().toString());
            cli.setEmail(edEmail.getText().toString());
            cli.setTelefone(Integer.valueOf(edTelefone.getText().toString()));
            cli.setEndereco(edEndereco.getText().toString());
            cli.setRg(Double.valueOf(edRg.getText().toString()));
            cli.setCpf(Double.valueOf(edCpf.getText().toString()));

            if (cli.getId() > 0) {
                getCliDao().alterar(cli);
            } else {
                getCliDao().inserirCliente(cli);
            }
            ToastManager.show(getBaseContext(), "Salvo com Sucesso",
                    ToastManager.INFORMATION);

        }

    });

    Button btnLimpar = (Button) findViewById(R.id.btLimpar);
    btnLimpar.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            montaTela(new Cliente());
        }
    });

    Button btExcluir = (Button) findViewById(R.id.btExcluir);
    btExcluir.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            final Cliente cli = new Cliente();
            cli.setId(Integer.valueOf(edId.getText().toString()));
            cli.setNome(edNome.getText().toString());
            cli.setEmail(edEmail.getText().toString());
            cli.setTelefone(Integer.valueOf(edTelefone.getText().toString()));
            cli.setEndereco(edEndereco.getText().toString());
            cli.setRg(Double.valueOf(edRg.getText().toString()));
            cli.setCpf(Double.valueOf(edCpf.getText().toString()));
            AlertDialog.Builder builder = new AlertDialog.Builder(CadCliente.this);
            builder.setTitle("Deseja Excluir?");
            builder.setMessage("O Cliente será deletado!");

            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    getCliDao().excluir(cli);
                    montaTela(new Cliente());
                    ToastManager.show(getBaseContext(), "Cliente excluído",
                            ToastManager.INFORMATION);

                }

            });

            builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    ToastManager.show(getBaseContext(), "Cancelado",
                            ToastManager.INFORMATION);
                }
            });

            AlertDialog alert = builder.create();
            alert.show();

        }
    });

}

private void montaTela(Cliente cliente) {
    edId.setText(String.valueOf(cliente.getId()));
    edNome.setText(cliente.getNome());
    edEmail.setText(cliente.getEmail());
    edTelefone.setText(String.valueOf(cliente.getTelefone()));
    edEndereco.setText(cliente.getEndereco());
    edCpf.setText(String.valueOf(cliente.getCpf()));
    edRg.setText(String.valueOf(cliente.getRg()));
}

public ClienteDao getCliDao() {
    if (cliDao == null) {
        cliDao = new ClienteDao();
    }
    return cliDao;
}

}
    
asked by anonymous 28.12.2015 / 06:19

2 answers

2

Hello!

I could not understand why a JavaNullPointerException runs, when in fact a NumberFormatException should occur.

Come on:

Where you should have a int you are passing the formatted number (51)3452-2470 so invalid int: occurs.

Before saving, you should remove the invalid characters!

  cli.setTelefone(Integer.valueOf(edTelefone.getText().toString()));

If the phone is an Integer field, an error of NumberFormatException should occur here.

Then to correct we will remove the mask:

 cli.setTelefone(Integer.valueOf(Mask.unmask(edTelefone.getText().toString())));

With Mask.unmask we pass only the numbers!

    
28.12.2015 / 11:43
1

I always use Integer.parseInt (String) to convert string to numbers, it can help

    
31.12.2015 / 09:41