Attempt to invoke virtual method 'long Contactodao.alterar(Contact)' on a null Object Reference

Asked

Viewed 282 times

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();
        }
    });
  • 1

    How you are instantiating the cDAO? Looks like it hasn’t been initialized

  • Private contact cDAO; //DAO class instance

  • 1

    Private contact cDAO.. not instantiated, missing a contact cDAO = new contact();

  • when I enter Contact cDAO = new Contact(); asks for the context, I put the beginning of the dao class up there

  • The context is its own class, that is, just put this where he asks for the context

  • Debugged and checked the instance of the objects involved? At least the line of the problem I think you can find

Show 1 more comment

1 answer

1


ERROR:

java.lang.Nullpointerexception: Attempt to invoke virtual method 'long costamilam.guilherme.contact us Press.ContactoDAO.change(costamilam.guilherme.contact us Press.Contact)' 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 'Contactodao.long alter (contact) ' in a null object reference

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

  long id = c.getId();

When you request on onClick to save the object, Id is not given.

If it is an object change, store this Id in a Class property, and when you save, enter:

  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 method alterar call the cadastrar:

  c.setAnoContato(spAnoContato.getSelectedItem().toString());
  // Contato não tem id, vamos cadastrar um novo!
  cDAO = new ContatoDAO(TelaDetalhe.this);
  cDAO.cadastrar(c);
  • I think I get it, but when I pass object c through it.putExtra(c, "contact"); object c already has id in case I just set the new value of the other variables. if I put a log. d(TAG, String.valueOf(c)); the id is not empty in case it has the value of 4

  • yes, always occurs regardless of id, the error occurs before arriving in the DAO class

  • the error is pointing to the line of cDAO.alter(c); and it does not arrive at this part because I put some Log. d in the DAO and Els class do not appear on the android monitor, IE, not even enter the DAO change method

  • Bele! what’s the name of the class? where’s this boot?

  • the name of the DAO class is contactDAO.java, the name of the contact update screen is Teladetalhe.java and the button is in activity_tela_detail.xml is this?

  • then do so before calling DAO cDAO = new Contactodao(Teladetalhe.this);

  • How are you inside the onClickListener to call Context uses if Teladetalhe.this I edited the answer to understand mehor

  • understood and worked only that now appears a new error similar to before only referring to the sql command line of the change

  • If the error is different, please open a new question! so it makes it easier for the next users to search on the site! ok?

  • already done so, link: https://answall.com/questions/236765/attempt-invoke-virtual-method-on-a-null-object-reference

Show 5 more comments

Browser other questions tagged

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