ListView of the Android database - Close app with error

1

I'm implementing a SQLite3 registry on Android and so far so good, the problem is in calling Activity of data listing when the registration is successful, when calling Activity the application is closed with error.

Follow the code below:

DebtorActivity

package br.com.savemoney.mastercontas;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import br.com.savemoney.database.DatabaseHelper;

public class DevedorActivity extends Activity {

  private DatabaseHelper helper;
  private EditText edtNomeDevedor, edtTelefoneDevedor, edtEmailDevedor;

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

    edtNomeDevedor = (EditText) findViewById(R.id.edtNomeDevedor);
    edtTelefoneDevedor = (EditText) findViewById(R.id.edtTelefoneDevedor);
    edtEmailDevedor = (EditText) findViewById(R.id.edtEmailDevedor);

    helper = new DatabaseHelper(this);
  }

  public void cadastrarDevedor(View view) {
    SQLiteDatabase db = helper.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("nome", edtNomeDevedor.getText().toString());
    values.put("telefone", edtTelefoneDevedor.getText().toString());
    values.put("email", edtEmailDevedor.getText().toString());

    long resultado = db.insert("devedor", null, values);
    if(resultado != -1) {
        Toast.makeText(getBaseContext(), R.string.cadastro_devedor_sucesso, Toast.LENGTH_LONG).show();
        startActivity(new Intent(this, ListaDevedoresActivity.class));
    } else {
        Toast.makeText(getBaseContext(), R.string.cadastro_devedor_erro, Toast.LENGTH_LONG).show();
    }
  }

  @Override
  protected void onDestroy() {
    helper.close();
    super.onDestroy();
  }
}

ListDepositorActivity

package br.com.savemoney.mastercontas;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import br.com.savemoney.database.DatabaseHelper;

public class ListaDevedoresActivity extends Activity {

  private DatabaseHelper helper;
  private SQLiteDatabase db;
  private SimpleCursorAdapter adapter;
  ListView listViewDevedores;

  private static final String[] campos = new String[] {"_id", "nome"};

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

    helper = new DatabaseHelper(this);

    listViewDevedores = (ListView) findViewById(R.id.listDevedores);

    listarDevedores();
  }

  public void listarDevedores(){
    //executa consulta geral de todos os registros cadastrados no banco de dados
    Cursor devedores = db.query("agenda", campos, null, null, null, null, null);

    if (devedores.getCount() > 0){
        //cria cursor que será exibido na tela, nele serão exibidos 
        //todos os contatos cadastrados
        adapter = new SimpleCursorAdapter(this, R.layout.item_devedor, devedores, campos, new int[] { R.id.txtIdDevedor, R.id.txtNomeDevedor});

        //relaciona o dataSource ao próprio listview
        listViewDevedores.setAdapter(adapter);
    }else{
        Toast.makeText(this, "Nenhum registro encontrado", Toast.LENGTH_SHORT).show(); 
    }
  }

  @Override
  protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();     

    //fecha a conexão com o Banco de dados
    db.close();        
   }
}

LogCat

Here, I just took the error part ... See if you can see anything!

> 06-19 14:21:08.579: E/AndroidRuntime(1971): FATAL EXCEPTION: main
> 06-19 14:21:08.579: E/AndroidRuntime(1971): Process:
> br.com.savemoney.mastercontas, PID: 1971 06-19 14:21:08.579:
> E/AndroidRuntime(1971): java.lang.RuntimeException: Unable to start
> activity
> ComponentInfo{br.com.savemoney.mastercontas/br.com.savemoney.mastercontas.ListaDevedoresActivity}:
> java.lang.NullPointerException 06-19 14:21:08.579:
> E/AndroidRuntime(1971):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
> 06-19 14:21:08.579: E/AndroidRuntime(1971):   at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
> 06-19 14:21:08.579: E/AndroidRuntime(1971):   at
> android.app.ActivityThread.access$800(ActivityThread.java:135) 06-19
> 14:21:08.579: E/AndroidRuntime(1971):     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
> 06-19 14:21:08.579: E/AndroidRuntime(1971):   at
> android.os.Handler.dispatchMessage(Handler.java:102) 06-19
> 14:21:08.579: E/AndroidRuntime(1971):     at
> android.os.Looper.loop(Looper.java:136) 06-19 14:21:08.579:
> E/AndroidRuntime(1971):   at
> android.app.ActivityThread.main(ActivityThread.java:5017) 06-19
> 14:21:08.579: E/AndroidRuntime(1971):     at
> java.lang.reflect.Method.invokeNative(Native Method) 06-19
> 14:21:08.579: E/AndroidRuntime(1971):     at
> java.lang.reflect.Method.invoke(Method.java:515) 06-19 14:21:08.579:
> E/AndroidRuntime(1971):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
> 06-19 14:21:08.579: E/AndroidRuntime(1971):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-19
> 14:21:08.579: E/AndroidRuntime(1971):     at
> dalvik.system.NativeStart.main(Native Method) 06-19 14:21:08.579:
> E/AndroidRuntime(1971): Caused by: java.lang.NullPointerException
> 06-19 14:21:08.579: E/AndroidRuntime(1971):   at
> br.com.savemoney.mastercontas.ListaDevedoresActivity.listarDevedores(ListaDevedoresActivity.java:35)
> 06-19 14:21:08.579: E/AndroidRuntime(1971):   at
> br.com.savemoney.mastercontas.ListaDevedoresActivity.onCreate(ListaDevedoresActivity.java:30)
> 06-19 14:21:08.579: E/AndroidRuntime(1971):   at
> android.app.Activity.performCreate(Activity.java:5231) 06-19
> 14:21:08.579: E/AndroidRuntime(1971):     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> 06-19 14:21:08.579: E/AndroidRuntime(1971):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
> 06-19 14:21:08.579: E/AndroidRuntime(1971):   ... 11 more

Can anyone see where the error is? Thank you in advance.

    
asked by anonymous 19.06.2015 / 19:08

1 answer

1

It is a NullPointerException , that is, there is a variable with a null value.

The best way to resolve runtime errors , such as the NullPointerException , is debugging the code.

Look in the stack for error references to your source files and the lines that caused the error.

For example: ExampleActivity: 96 .

Put a breakpoint before the indicated line (96), run the debug, and inspect the values of your variables, step by step.

It's not worth assuming what happened, it's easier to leave right away for action.

TIP : In onCreate, instead of retrieving only the reference of your visual components (findViewById), also treat your values.

Example:

EditText edtNomeDevedor = (EditText) edtNomeDevedor = (EditText) findViewById(R.id.edtNomeDevedor);
String nomeDevedor = edtNomeDevedor.getText().toString() == null ? "": edtNomeDevedor.getText().toString();

In this way you treat the value, which may be null, already in the creation of your activity (Activity).

Good luck!

    
20.06.2015 / 14:08