java.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.NullPointerException

0

Hello, I'm new to android, and when I try to run my app, these errors:

 08-15 18:10:49.828: E/AndroidRuntime(1769): FATAL EXCEPTION: main
08-15 18:10:49.828: E/AndroidRuntime(1769): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.example.ifagenda/br.com.example.ifagenda.PessoaPrincipal}: java.lang.NullPointerException
08-15 18:10:49.828: E/AndroidRuntime(1769):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-15 18:10:49.828: E/AndroidRuntime(1769):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-15 18:10:49.828: E/AndroidRuntime(1769):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-15 18:10:49.828: E/AndroidRuntime(1769):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-15 18:10:49.828: E/AndroidRuntime(1769):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 18:10:49.828: E/AndroidRuntime(1769):     at android.os.Looper.loop(Looper.java:130)
08-15 18:10:49.828: E/AndroidRuntime(1769):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-15 18:10:49.828: E/AndroidRuntime(1769):     at java.lang.reflect.Method.invokeNative(Native Method)
08-15 18:10:49.828: E/AndroidRuntime(1769):     at java.lang.reflect.Method.invoke(Method.java:507)
08-15 18:10:49.828: E/AndroidRuntime(1769):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-15 18:10:49.828: E/AndroidRuntime(1769):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-15 18:10:49.828: E/AndroidRuntime(1769):     at dalvik.system.NativeStart.main(Native Method)
08-15 18:10:49.828: E/AndroidRuntime(1769): Caused by: java.lang.NullPointerException
08-15 18:10:49.828: E/AndroidRuntime(1769):     at br.com.example.ifagenda.PessoaConexaoBanco.listarPessoas(PessoaConexaoBanco.java:46)
08-15 18:10:49.828: E/AndroidRuntime(1769):     at br.com.example.ifagenda.PessoaPrincipal.onCreate(PessoaPrincipal.java:16)
08-15 18:10:49.828: E/AndroidRuntime(1769):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-15 18:10:49.828: E/AndroidRuntime(1769):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-15 18:10:49.828: E/AndroidRuntime(1769):     ... 11 more

What can it be?

package br.com.example.ifagenda;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;

public class PessoaPrincipal extends ListActivity {
    public static PessoaConexaoBanco pessoaConexaoBanco;
    private List<Pessoa> pessoas;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        pessoaConexaoBanco = new PessoaConexaoBanco(this);
        pessoas = pessoaConexaoBanco.listarPessoas();
        // customizando para inserir linha a linha os registros das pessoas
        setListAdapter(new PessoaListAdapter(this, pessoas));
    }
}
    package br.com.example.ifagenda;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class PessoaConexaoBanco {
    protected SQLiteDatabase bancoDados = null;

    public PessoaConexaoBanco(Context context) {
        bancoDados = context.openOrCreateDatabase("dbalunos", Context.MODE_PRIVATE, null);
        String sqlCriaTabela = " CREATE TABLE IF NOT EXISTS pessoas" +
                "(_id INTEGER PRIMARY KEY autoincrement, nome TEXT , endereco TEXT" +
                "telefone TEXT);";
        bancoDados.execSQL(sqlCriaTabela);
        //String adicionarRegistros = "insert into pessoas (nome, endereco, telefone) values" +
            //  "('Gabriel','Rua Gibaut','123456')";
        //bancoDados.execSQL(adicionarRegistros);

    }
    // metodo que retorna as pessoas da db
        public Cursor getCursor(){
            try {
                return bancoDados.query("pessoas",
                        //new String[] {"nome", "endereco","telefone"},
                        //or do
                        Pessoa.colunas,
                        null,//selection,
                        null,//selectionArgs,
                        null,//groupBy,
                        null,//having
                        null,//"order by nome"//orderBy)
                        null); //Limite de registros retornados
            } catch (SQLException erro) {
                return null;
            }
        }
        // metodo que retornará a lista das pessoas
        public List<Pessoa> listarPessoas() {
            Cursor cursor = getCursor();
            List<Pessoa> listPessoas = new ArrayList<Pessoa>();
            if (cursor.moveToFirst()) {
                int indexId = cursor.getColumnIndex(Pessoa._ID);
                int indexNome = cursor.getColumnIndex(Pessoa.NOME);
                int indexEndereco = cursor.getColumnIndex(Pessoa.ENDERECO);
                int indexTelefone = cursor.getColumnIndex(Pessoa.TELEFONE);
                do{
                    Pessoa pessoa = new Pessoa();
                    listPessoas.add(pessoa);
                    pessoa.id = cursor.getLong(indexId);
                    pessoa.nome = cursor.getString(indexNome);
                    pessoa.endereco = cursor.getString(indexEndereco);
                    pessoa.telefone = cursor.getString(indexTelefone);
                }
                while (cursor.moveToNext());
            }
            return listPessoas;
    }
}

Now it appeared:

08-16 22:42:44.823: W/System.err(1374): android.database.sqlite.SQLiteException: no such column: telefone: , while compiling: SELECT _id, nome, endereco, telefone FROM pessoas
08-16 22:42:44.823: W/System.err(1374):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
08-16 22:42:44.823: W/System.err(1374):     at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
08-16 22:42:44.823: W/System.err(1374):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
08-16 22:42:44.823: W/System.err(1374):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
08-16 22:42:44.823: W/System.err(1374):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
08-16 22:42:44.823: W/System.err(1374):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
08-16 22:42:44.823: W/System.err(1374):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
08-16 22:42:44.823: W/System.err(1374):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1235)
08-16 22:42:44.823: W/System.err(1374):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1189)
08-16 22:42:44.823: W/System.err(1374):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1309)
08-16 22:42:44.823: W/System.err(1374):     at br.com.example.ifagenda.PessoaConexaoBanco.getCursor(PessoaConexaoBanco.java:29)
08-16 22:42:44.823: W/System.err(1374):     at br.com.example.ifagenda.PessoaConexaoBanco.listarPessoas(PessoaConexaoBanco.java:46)
08-16 22:42:44.823: W/System.err(1374):     at br.com.example.ifagenda.PessoaPrincipal.onCreate(PessoaPrincipal.java:16)
08-16 22:42:44.823: W/System.err(1374):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-16 22:42:44.823: W/System.err(1374):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-16 22:42:44.823: W/System.err(1374):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-16 22:42:44.823: W/System.err(1374):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-16 22:42:44.823: W/System.err(1374):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-16 22:42:44.823: W/System.err(1374):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 22:42:44.823: W/System.err(1374):     at android.os.Looper.loop(Looper.java:130)
08-16 22:42:44.823: W/System.err(1374):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-16 22:42:44.823: W/System.err(1374):     at java.lang.reflect.Method.invokeNative(Native Method)
08-16 22:42:44.823: W/System.err(1374):     at java.lang.reflect.Method.invoke(Method.java:507)
08-16 22:42:44.823: W/System.err(1374):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-16 22:42:44.823: W/System.err(1374):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-16 22:42:44.823: W/System.err(1374):     at dalvik.system.NativeStart.main(Native Method)
08-16 22:42:44.823: D/AndroidRuntime(1374): Shutting down VM
08-16 22:42:44.823: W/dalvikvm(1374): threadid=1: thread exiting with uncaught exception (group=0xb58394f0)
    
asked by anonymous 17.08.2014 / 01:51

1 answer

2

I believe the error is in your table creation command.

You are concatenating the declaration of column telefone with no comma. I do not know how it did not generate syntax error.

With the current code your command would look like:

  

CREATE TABLE IF NOT EXISTS people (_id INTEGER PRIMARY KEY autoincrement, TEXT name, TEXT address TEXT);

Exchanging the code for:

public PessoaConexaoBanco(Context context) {
    bancoDados = context.openOrCreateDatabase("dbalunos", Context.MODE_PRIVATE, null);
    String sqlCriaTabela = " CREATE TABLE IF NOT EXISTS pessoas" +
            "(_id INTEGER PRIMARY KEY autoincrement, nome TEXT, endereco TEXT" +
            ", telefone TEXT);";
    bancoDados.execSQL(sqlCriaTabela);
    //String adicionarRegistros = "insert into pessoas (nome, endereco, telefone) values" +
        //  "('Gabriel','Rua Gibaut','123456')";
    //bancoDados.execSQL(adicionarRegistros);

}

The problem must be solved.

    
17.08.2014 / 04:09