-1
Error:
09-12 17:13:09.088 26022-26022/costamilam.guilherme.contatosempresariais E/AndroidRuntime: FATAL EXCEPTION: main Process: costamilam.guilherme.contatosempresariais, PID: 26022 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.update(java.lang.String, android.content.ContentValues, java.lang.String, java.lang.String[])' on a null object reference at costamilam.guilherme.contatosempresariais.ContatoDAO.alterar(ContatoDAO.java:83) at costamilam.guilherme.contatosempresariais.TelaDetalhe$1.onClick(TelaDetalhe.java:153) 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) 09-12 17:26:05.946 26198-26198/costamilam.guilherme.contatosempresariais E/AndroidRuntime: FATAL EXCEPTION: main Process: costamilam.guilherme.contatosempresariais, PID: 26198 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.delete(java.lang.String, java.lang.String, java.lang.String[])' on a null object reference at costamilam.guilherme.contatosempresariais.ContatoDAO.deletar(ContatoDAO.java:90) at costamilam.guilherme.contatosempresariais.TelaDetalhe$2.onClick(TelaDetalhe.java:172) 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)
Canvas:
public class TelaDetalhe extends AppCompatActivity {
private ContatoDAO cDAO; //Instancia da classe DAO
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tela_detalhe);
final Contato c = (Contato) getIntent().getSerializableExtra("contato");
btnAtualizar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
long contatoId = c.getId();
c.setId(contatoId);
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 = new ContatoDAO(TelaDetalhe.this);
cDAO.alterar(c);
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 = new ContatoDAO(TelaDetalhe.this);
cDAO.deletar(c.getId());
Toast.makeText(getBaseContext(), "Contato excluído com sucesso!", Toast.LENGTH_LONG).show();
}
});
}
}
DAO:
public class ContatoDAO {
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();
}
//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)});
}
}
Please, we suggest you perform a tour by the site and see How to create a Minimum, Complete and Verifiable example! So make it easier for the community to help you!
– Thiago Luiz Domacoski
I think it’s been organized now
– Costamilam
This Nullpointerexception is an indication that you have not started with
abrirBanco
or has already closed thefecharBanco
– Jefferson Quesado
That was the problem, when I changed it to a new screen (before it was an Alert) I forgot to open the bank. Thank you Jefferson Quesado
– Costamilam
The problem is similar but different Carlos Heuberger, in the first absences instantiate the DAO class and here the problem was because the bank was not opened
– Costamilam