java.lang.Runtimeexception: Unable to start Activity Componentinfo java.lang.Nullpointerexception

Asked

Viewed 359 times

0

Hello, I’m new to android and when I try to run my application appears these following mistakes:

 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 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)
  • Could include the class PessoaConexaoBanco?

  • ready, already put...

  • You’re having a SQLException within the method getCursor. Give a printStackTrace in this exception to see the error in the query.

1 answer

2

I believe the error is in your table creation command.

Is concatenating the column statement telefone comma-free. I don’t know how it didn’t generate syntax error.

With the current code your command would be:

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

Changing the code to:

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 solving.

Browser other questions tagged

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