3
Well I am developing this app, to be able to display the information of a user in an arraylist, I run the code and it simply hangs and closes the app, I reviewed the code and found nothing, some know where the error is?
It shows this error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.matheus.kypyy/com.example.matheus.kypyy.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method
'java.util.ArrayList com.example.matheus.kypyy.FireHelper.retorno()' on a
null object reference
Thank you in advance...
//Class of Users
public class Usuarios {
private String nome;
private String Data;
private String Rg;
private String Cpf;
private String Endereco;
private String Doenca;
private String Profissao;
public void Usuarios(){
}
public String getCpf() {
return Cpf;
}
public void setCpf(String cpf) {
Cpf = cpf;
}
public String getData() {
return Data;
}
public void setData(String data) {
Data = data;
}
public String getDoenca() {
return Doenca;
}
public void setDoenca(String doenca) {
Doenca = doenca;
}
public String getEndereco() {
return Endereco;
}
public void setEndereco(String endereco) {
Endereco = endereco;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getProfissao() {
return Profissao;
}
public void setProfissao(String profissao) {
Profissao = profissao;
}
public String getRg() {
return Rg;
}
public void setRg(String rg) {
Rg = rg;
}
}
//Firebase BD Class
public class FireHelper {
DatabaseReference bd;
Boolean saved;
ArrayList<Usuarios> usuarioss = new ArrayList<>();
public FireHelper(DatabaseReference bd) {
this.bd = bd;
}
public Boolean save(Usuarios usuarios){
if(usuarios == null){
saved = false;
}
else
{
try{
bd.child("Usuarios").push().setValue(usuarios);
}catch (DatabaseException e){
e.printStackTrace();
saved = false;
}
}
return saved;
}
private void fetchData(DataSnapshot dataSnapshot){
usuarioss.clear();
for(DataSnapshot ds : dataSnapshot.getChildren()){
Usuarios usuarios = ds.getValue(Usuarios.class);
usuarioss.add(usuarios);
}
}
public ArrayList<Usuarios> retorno(){
bd.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return usuarioss;
}
}
// Main class where the data will be displayed
public class MainActivity extends AppCompatActivity {
DatabaseReference bd;
FireHelper helper;
AdapterCustom adapter;
ListView lv;
EditText textViewNome,textViewData,textViewRg,
textViewCpf,textViewEndereco,textViewDoenca,textViewProfissao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//layout
lv = (ListView) findViewById(R.id.lv);
//Adaptador
adapter = new AdapterCustom(this, helper.retorno());
lv.setAdapter(adapter);
//Banco de dados
bd = FirebaseDatabase.getInstance().getReference();
helper = new FireHelper(bd);
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
displayInputDialog();}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
private void displayInputDialog(){
Dialog d = new Dialog(this);
d.setTitle("Savo no banco de dados");
d.setContentView(R.layout.input_dialog);
//Declarando objetos
final TextView textViewNome =
(TextView)d.findViewById(R.id.editTextNome);
final TextView textViewData =
(TextView)d.findViewById(R.id.editTextData);
final TextView textViewRg = (TextView)d.findViewById(R.id.editTextRg);
final TextView textViewCpf = (TextView)d.findViewById(R.id.editTextCpf);
final TextView textViewEndereco =
(TextView)d.findViewById(R.id.editTextEndereco);
final TextView textViewDoenca =
(TextView)d.findViewById(R.id.editTextDoenca);
final TextView textViewProfissao =
(TextView)d.findViewById(R.id.editTextProfissao);
Button saveButton = (Button)d.findViewById(R.id.savebutton);
//Salvar...
saveButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String nome = textViewNome.getText().toString();
String data = textViewData.getText().toString();
String rg = textViewRg.getText().toString();
String cpf = textViewCpf.getText().toString();
String endereco = textViewEndereco.getText().toString();
String doenca = textViewDoenca.getText().toString();
String profissao = textViewProfissao.getText().toString();
Usuarios u = new Usuarios();
u.setNome(nome);
u.setData(data);
u.setRg(rg);
u.setCpf(cpf);
u.setEndereco(endereco);
u.setDoenca(doenca);
u.setProfissao(profissao);
if(nome != null && nome.length()> 0){
if(helper.save(u)){
textViewNome.setText("");
textViewData.setText("");
textViewRg.setText("");
textViewCpf.setText("");
textViewEndereco.setText("");
textViewDoenca.setText("");
textViewProfissao.setText("");
adapter = new AdapterCustom(MainActivity.this,
helper.retorno());
lv.setAdapter(adapter);
}else {
Toast.makeText(MainActivity.this, "sem dados",
Toast.LENGTH_SHORT).show();
}
}
}});
}
}
//Class of the Adapter
public class AdapterCustom extends BaseAdapter {
Context c;
ArrayList<Usuarios> usuarioss;
public AdapterCustom(Context c, ArrayList<Usuarios> usuarioss) {
this.c = c;
this.usuarioss = usuarioss;
}
@Override
public int getCount() {
return usuarioss.size();
}
@Override
public Object getItem(int position) {
return usuarioss.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView== null){
convertView = LayoutInflater.from(c).inflate(R.layout.model,parent,false);
}
//Declarando objetos
TextView textViewNome = (TextView)convertView.findViewById(R.id.textViewNome);
TextView textViewData = (TextView)convertView.findViewById(R.id.textViewData);
TextView textViewRg = (TextView)convertView.findViewById(R.id.textViewRg);
TextView textViewCpf = (TextView)convertView.findViewById(R.id.textViewCpf);
TextView textViewEndereco = (TextView)convertView.findViewById(R.id.textViewEndereco);
TextView textViewDoenca = (TextView)convertView.findViewById(R.id.textViewDoenca);
TextView textViewProfissao = (TextView)convertView.findViewById(R.id.textViewProfissao);
final Usuarios u = (Usuarios) this.getItem(position);
textViewNome.setText(u.getNome());
textViewData.setText(u.getData());
textViewRg.setText(u.getRg());
textViewCpf.setText(u.getCpf());
textViewEndereco.setText(u.getEndereco());
textViewDoenca.setText(u.getDoenca());
textViewProfissao.setText(u.getProfissao());
//Função onclick
convertView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(c, u.getNome(), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
In the method
onCreate
classMainActivity
you useadapter = new AdapterCustom(this, helper.retorno());
, but does not start the class, you have only the statementFireHelper helper
, that is to sayhelper == null
– Marco Giovanni