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)});
}
Refresh 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();
}
});
How you are instantiating the cDAO? Looks like it hasn’t been initialized
– Leonardo Dias
Private contact cDAO; //DAO class instance
– Costamilam
Private contact cDAO.. not instantiated, missing a contact cDAO = new contact();
– Dev
when I enter Contact cDAO = new Contact(); asks for the context, I put the beginning of the dao class up there
– Costamilam
The context is its own class, that is, just put this where he asks for the context
– Leonardo Dias
Debugged and checked the instance of the objects involved? At least the line of the problem I think you can find
– André Ozawa