Error save data from sqlite register

Asked

Viewed 187 times

3

I am in error when saving data from a people register in Javanullpointerexception sqlite: invalid int:(51)3452-2470. This error started to occur after I applied a mask. Follow 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 registration 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;
}

}
  • put the mask in one field only and check if it gives error again.

2 answers

2


Hello !

I couldn’t understand why the one was running JavaNullPointerException, when there should actually be a NumberFormatException.

Come on:

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

Before saving, you must remove invalid characters!

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

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

So to correct we will remove the mask:

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

With the Mask.unmask we just pass the numbers!

  • Hello Thiago Luiz.. I did exactly what you suggested and in fact did not show the same error. However, another error occurred. In Logs here only shows that on the line where I made the change, as suggested, is pointing invalid int error but does not show the phone as it occurred before.

  • Can you show the Log? Change your question and add this information! is easier to help you!

  • See if this error is not occurring with Rg or CPF, as they also have masks!

  • No no.. the error is exactly on the phone number.. invalid int appears without the mask on the log screen, your help worked because it removed the mask..

  • Try debugging and see how the number is going

  • Going like this: Numberformatexception: invalid int "5196487698"

  • It’s a very large value for an int. I suggest you use String for both phone, RG and CPF.

Show 2 more comments

1

Always use Integer.parseint(String) to convert string into numbers, can help

Browser other questions tagged

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