0

I am having a SQLITE Android problem.

I am using the same activity [CadastroCliente] to both Register & Update customers information; To define whether I want to update or register, I have a public static integer variable called UPDATE in the main menu [ListarClientes], which 0 = register and 1 = update.

So it goes like this:

  1. the user clicks on "edit info"
  2. I tell the register/update activity that the UPDATE variable value is 1, so it knows it's on editing mode.
  3. the user type in the stuff he wants to update.
  4. When he clicks the "save" button, this is the method I call:

            if (listarCliente.UPDATE == 0)
                this.cadastrar();
            if (listarCliente.UPDATE == 1)
                this.clienteDAO.updateData(listarCliente.ID_CLIENTE, nome, email);
    

And this is the error I get:

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 dao.ClienteDAO.updateData(ClienteDAO.java:104)
at com.financeiro.coolkey.financeiro_2.CadastroCliente.onOptionsItemSelected(CadastroCliente.java:138)

I am confused because when I call "clienteDAO.updateData" from ListarCliente activity it works like a charm. But when I send it to CadastroCliente, it returns this error. Any tips?

If it's lacking any info please tell me and I will definitely update! :)

Thanks in advance!!!

-

- EXTRA INFO:

dao.ClienteDAO.updateData(ClienteDAO.java:104)

public boolean updateData(long updateId, String nome, String email)
{
    ContentValues contentValues = new ContentValues();
    contentValues.put(DatabaseHelper.Clientes.NOME, nome);
    contentValues.put(DatabaseHelper.Clientes.EMAIL, email);

    return database.update(DatabaseHelper.Clientes.TABELA, contentValues, DatabaseHelper.Clientes._ID + "=" + updateId, null) > 0;
}

com.financeiro.coolkey.financeiro_2.CadastroCliente.onOptionsItemSelected(CadastroCliente.java:138)

this.clienteDAO.updateData(listarCliente.ID_CLIENTE, nome, email);

How the activity is started from the ListarClientes [main menu]:

        UPDATE = 1;
        startActivity(new Intent(ListarCliente.this, CadastroCliente.class));

Complete onClick method where the updateData is called:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    int id = (int) item.getItemId();

    AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

    String nome = edtNome.getText().toString();
    String email = edtEmail.getText().toString();

    if (id == R.id.action_menu_salvar)
    {
        validacao = true;

        if (nome == null || nome.equals(""))
        {
            validacao = false;
            edtNome.setError(getString(R.string.mensagem_cadastro_erro));
        }

        if(email == null || email.equals(""))
        {
            validacao = false;
            edtEmail.setError(getString(R.string.mensagem_cadastro_erro));
        }

        if(validacao)
        {
            if (listarCliente.UPDATE == 0)
                this.cadastrar();
            if (listarCliente.UPDATE == 1)
                this.clienteDAO.updateData(listarCliente.ID_CLIENTE, nome, email);


            finish();
            startActivity(new Intent(this, ListarCliente.class));
        }
    }

    if (id == R.id.action_menu_sair)
    {
        finish();
        startActivity(new Intent(this, ListarCliente.class));
    }

    return super.onOptionsItemSelected(item);
}

WHERE clienteDAO is initialized:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cadastro_cliente);

    if (listarCliente.UPDATE == 0)
        setTitle("Cadastro de Clientes");
    else if (listarCliente.UPDATE == 1)
        setTitle("Editar Dados do cliente");


    clienteDAO = new ClienteDAO(this);


    edtNome = (EditText) findViewById(R.id.cadastro_edtNome);
    edtEmail = (EditText) findViewById(R.id.cadastro_edtEmail);
}
5
  • Please post more code, e.g. where the save method is located in or where you initialize clienteDAO. Commented Jan 23, 2015 at 18:46
  • I updated the question, thank you for feedback! If you still need more info please let me know :) Commented Jan 23, 2015 at 18:53
  • Probably database is null where database is initialized ? Commented Jan 23, 2015 at 19:03
  • MAN! you made it work! Thank you so much @ρяσѕρєя K!! I just had to initialize my database helper at "onCreate" and it is working just fine. Peace out brother! thanks again! :) Commented Jan 23, 2015 at 19:18
  • Can you please write the answer so I select you as the best answer? Commented Jan 23, 2015 at 19:18

1 Answer 1

3

NullPointerException: Attempt to invoke virtual method

Means update method is called using non-initialized object. so make sure database is not null before calling update method

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! For everyone else who has the same problem, I have a DatabaseHelper file and I had to instantiate it on the onCreate method of my activity :) private DatabaseHelper databaseHelper; databaseHelper = new DatabaseHelper(this);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.