rodrigo.oliveira rodrigo.oliveira - 1 month ago 12
Android Question

how to load data from a sqlite table for several EDITTEXT

I'm having trouble to get this data and display on an activity, can someone help me? How Can I Call DAO, and pass this data ?

all trace of classes used so far to get this problem.

Class model

@DatabaseTable(tableName = "pessoa")
public class Pessoa implements EntidadePersitivel {
@DatabaseField(generatedId = true)
private int id;
@DatabaseField
private int codigo;
@DatabaseField
private String nome;
@DatabaseField
private String cnpjCpf;


public void setUf(String uf) {
this.uf = uf;
}

@DatabaseField
private String rgIE;
@DatabaseField
private String cep;
@DatabaseField
private String uf;
@DatabaseField
private String cidade;
@DatabaseField
private String bairro;
@DatabaseField
private String endereco;
@DatabaseField
private String numero;
@DatabaseField
private String complemento;
@DatabaseField
private String foneComercial;
@DatabaseField
private String foneResidencial;
@DatabaseField
private String foneCelular;
@DatabaseField
private String email;


public Pessoa(int id, int codigo, String nome, String cnpjCpf, String rgIE,
String cep, String cidade, String bairro, String endereco,
String numero, String complemento, String foneComercial, String foneResidencial, String foneCelular, String email) {
this.id = id;
this.codigo = codigo;
this.nome = nome;
this.cnpjCpf = cnpjCpf;
this.rgIE = rgIE;
this.cep = cep;

this.cidade = cidade;
this.bairro = bairro;
this.endereco = endereco;
this.numero = numero;
this.complemento = complemento;
this.foneComercial = foneComercial;
this.foneResidencial = foneResidencial;
this.foneCelular = foneCelular;
this.email = email;
}

public Pessoa() {

}


class DAO

*/

public class PessoaDao extends BaseDaoImpl<Pessoa, Integer> {
public PessoaDao(ConnectionSource connectionSource) throws SQLException {
super(Pessoa.class);
setConnectionSource(connectionSource);
initialize();


}

}


class dataBaseHelper

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

private static final String dataBaseName= "central";
private static final int version =1;

public DatabaseHelper(Context context) {
super(context, dataBaseName, null, version);
}


@Override
public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {

try {
TableUtils.createTable(connectionSource,LoginSeralizable.class);
TableUtils.createTable(connectionSource, Pessoa.class);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
TableUtils.dropTable(connectionSource, LoginSeralizable.class,true);
TableUtils.dropTable(connectionSource, Pessoa.class, true);

onCreate(sqLiteDatabase, connectionSource);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}

}

@Override
public void close(){
super.close();
}
}


error log

FATAL EXCEPTION: Thread-284

Process: routerbox.com.br.centraisdoassinante, PID: 25202
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object routerbox.com.br.centraisdoassinante.routerbox.com.br.centraisdoassinante.Dao.PessoaDao.queryForId(java.lang.Object)' on a null object reference
at routerbox.com.br.centraisdoassinante.DadosCadastrais$1.run(DadosCadastrais.java:49)
at java.lang.Thread.run(Thread.java:818)

Answer

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object routerbox.com.br.centraisdoassinante.routerbox.com.br.centraisdoassinante.Dao.PessoaDao.queryForId(java.lang.Object)' on a null object reference

You are trying to execute the method queryForId in a null object, what this mean ?

The PessoaDao is a non static class, what means that you need to instantiate it before you can use they methods.

So you have deleted the part of your code where you call this function but if i remember right you have something like that :

PessoaDao.queryForId('1');

but you need to do :

PessoaDao pessoaDao = new PessoaDao(yourConnectionSource);
pessoaDao.queryForId('1');

Btw, i suppose Pessoa is portuguese right ? Maybe you can ask in the Stack Overflow in portuguese, this way i can answer you better, and you will not need to deal with my bad english :)