Attempt to invoke virtual method 'long ContactID.change (Contact)' on a null object reference [closed]

0

Error:

09-12 14:05:28.662 10410-10410/costamilam.guilherme.contatosempresariais E/AndroidRuntime: FATAL EXCEPTION: main
Process: costamilam.guilherme.contatosempresariais, PID: 10410
java.lang.NullPointerException: Attempt to invoke virtual method 'long costamilam.guilherme.contatosempresariais.ContatoDAO.alterar(costamilam.guilherme.contatosempresariais.Contato)' on a null object reference
at costamilam.guilherme.contatosempresariais.TelaDetalhe$1.onClick(TelaDetalhe.java:158)
at android.view.View.performClick(View.java:5076)
at android.view.View$PerformClick.run(View.java:20279)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

DAO:

public class ContatoDAO {
public static final String TAG = "LogDAO";

private SQLiteDatabase database;
private BaseDAO dbHelper;

public ContatoDAO(Context contexto){
    dbHelper = new BaseDAO(contexto);
}

public void abrirBanco() {
    database = dbHelper.getWritableDatabase();
}

public void fecharBanco() {
    dbHelper.close();
}

//Cadastrar
public long cadastrar(Contato c) {
    ContentValues cv = new ContentValues();

    cv.put(BaseDAO.CONTATO_NOME, c.getNome());
    cv.put(BaseDAO.CONTATO_TELEFONE, c.getTelefone());
    cv.put(BaseDAO.CONTATO_EMPRESA, c.getEmpresa());
    cv.put(BaseDAO.CONTATO_BRUNKO, c.isBrunko());
    cv.put(BaseDAO.CONTATO_CARTER, c.isCarter());
    cv.put(BaseDAO.CONTATO_FORTMETAL, c.isFortmetal());
    cv.put(BaseDAO.CONTATO_MERCO, c.isMerco());
    cv.put(BaseDAO.CONTATO_METAN, c.isMetan());
    cv.put(BaseDAO.CONTATO_NDFLEX, c.isNdflex());
    cv.put(BaseDAO.CONTATO_NOTUS, c.isNotus());
    cv.put(BaseDAO.CONTATO_RIOSULENSE, c.isRiosulense());
    cv.put(BaseDAO.CONTATO_YIMING, c.isYming());
    cv.put(BaseDAO.CONTATO_MES_CONTATO, c.getMesContato());
    cv.put(BaseDAO.CONTATO_ANO_CONTATO, c.getAnoContato());

    Log.d(TAG, String.valueOf(c.isBrunko()));

    return database.insert(BaseDAO.TABELA_AGENDA, null, cv);
}

//Alterar
public long alterar(Contato c) {

    long id = c.getId();

    ContentValues cv = new ContentValues();

    cv.put(BaseDAO.CONTATO_NOME, c.getNome());
    cv.put(BaseDAO.CONTATO_TELEFONE, c.getTelefone());
    cv.put(BaseDAO.CONTATO_EMPRESA, c.getEmpresa());
    cv.put(BaseDAO.CONTATO_BRUNKO, c.isBrunko());
    cv.put(BaseDAO.CONTATO_CARTER, c.isCarter());
    cv.put(BaseDAO.CONTATO_FORTMETAL, c.isFortmetal());
    cv.put(BaseDAO.CONTATO_MERCO, c.isMerco());
    cv.put(BaseDAO.CONTATO_METAN, c.isMetan());
    cv.put(BaseDAO.CONTATO_NDFLEX, c.isNdflex());
    cv.put(BaseDAO.CONTATO_NOTUS, c.isNotus());
    cv.put(BaseDAO.CONTATO_RIOSULENSE, c.isRiosulense());
    cv.put(BaseDAO.CONTATO_YIMING, c.isYming());
    cv.put(BaseDAO.CONTATO_MES_CONTATO, c.getMesContato());
    cv.put(BaseDAO.CONTATO_ANO_CONTATO, c.getAnoContato());

    return database.update(BaseDAO.TABELA_AGENDA, cv, BaseDAO.CONTATO_ID + "=?", new String[]{String.valueOf(id)});
}

//Deletar
public long deletar(long id) {
    long identificador = id;

    return database.delete(BaseDAO.TABELA_AGENDA, BaseDAO.CONTATO_ID + "=?", new String[]{String.valueOf(identificador)});
}

Update screen:

    btnAtualizar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            c.setNome(etNome.getText().toString());
            c.setTelefone(etTelefone.getText().toString());
            c.setEmpresa(etEmpresa.getText().toString());
            c.setBrunko(cbBrunko.isChecked());
            c.setCarter(cbCarter.isChecked());
            c.setFortmetal(cbFortmetal.isChecked());
            c.setMerco(cbMerco.isChecked());
            c.setMetan(cbMetan.isChecked());
            c.setNdflex(cbNdflex.isChecked());
            c.setNotus(cbNotus.isChecked());
            c.setRiosulense(cbRiosulense.isChecked());
            c.setYming(cbYiming.isChecked());
            c.setMesContato(spMesContato.getSelectedItemPosition());
            c.setAnoContato(spAnoContato.getSelectedItem().toString());

            cDAO.alterar(c);

            adapter.notifyDataSetChanged();

            Toast.makeText(getBaseContext(), "Contato alterado com sucesso!", Toast.LENGTH_LONG).show();
        }
    });

    btnDeletar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Removendo do Banco
            cDAO.deletar(c.getId());

            Toast.makeText(getBaseContext(), "Contato excluído com sucesso!", Toast.LENGTH_LONG).show();
        }
    });
    
asked by anonymous 12.09.2017 / 19:21

1 answer

1

ERROR:

  

java.lang.NullPointerException: Attempt to invoke virtual method 'long   costamilam.guilherme.contatosempresariais.ContatoDAO.alterar (costamilam.guilherme.contatosconpresosis.Contato) '   on a null object reference

The NullPointerException is when the system tries to access a reference that is null!

Error free translation:

  

Attempt to invoke virtual method 'ADO.Change long   (Contact) 'in a null object reference

So you're trying to pass null to a primitive object long . This occurs on the following line:

  long id = c.getId();

When you ask in onClick to save the object, the Id is not informed.

If this is an object change, save this Id in a Class property, and when saving, report:

  c.setAnoContato(spAnoContato.getSelectedItem().toString());
  c.setId(idContato); // Id do contato que está sendo editado!
  cDAO = new ContatoDAO(TelaDetalhe.this);
  cDAO.alterar(c);

Now, if it is an inclusion (a new record), instead of calling the alterar method call cadastrar :

  c.setAnoContato(spAnoContato.getSelectedItem().toString());
  // Contato não tem id, vamos cadastrar um novo!
  cDAO = new ContatoDAO(TelaDetalhe.this);
  cDAO.cadastrar(c);
    
12.09.2017 / 19:46