1

As I am able to retrieve data from Firebase, but for single user only database in android. I just want to get the data from database which I have inserted in firebase without user authentication login.

I am getting error in logcat:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

Here is my code:

import android.os.Bundle;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class ViewDatabase extends AppCompatActivity {
 private static final String TAG = "ViewDatabase";

 private DatabaseReference databaseReference;
 private String userID;

 private ListView mListView;

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

  mListView = (ListView) findViewById(R.id.listview);
  databaseReference = FirebaseDatabase.getInstance().getReference();



  databaseReference.addValueEventListener(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot dataSnapshot) {
    showData(dataSnapshot);
   }

   @Override
   public void onCancelled(DatabaseError databaseError) {

   }
  });

 }

 private void showData(DataSnapshot dataSnapshot) {
  userinfo user = dataSnapshot.getValue(userinfo.class);

  Log.d(TAG, "showData: name: " + user.getName());
  Log.d(TAG, "showData: Mobile: " + user.getMob());
  Log.d(TAG, "showData: Vehicle: " + user.getVehicle());
  Log.d(TAG, "showData: Address: " + user.getaddress());

  ArrayList < String > array = new ArrayList < > ();
  array.add(user.getName());
  array.add(user.getMob());
  array.add(user.getVehicle());
  array.add(user.getaddress());
  ArrayAdapter adapter = new
  ArrayAdapter(this, android.R.layout.simple_list_item_1, array);
  mListView.setAdapter(adapter);
 }


}

Here is my userinfo code:

  public class userinfo {

   public String name;
   public String mob;
   public String vehicle;
   public String address;

   public userinfo() {

   }

   public userinfo(String name, String mob, String vehicle, String address) {
    this.name = name;
    this.mob = mob;
    this.vehicle = vehicle;
    this.address = address;
   }
   public String getName() {
    return name;
   }
   public String getMob() {
    return mob;
   }
   public String getVehicle() {
    return vehicle;
   }
   public String getaddress() {
    return address;
   }
  }
  }
stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
Parth Tiwari
  • 466
  • 8
  • 23

1 Answers1

0

Using an iterator you will be able to accomplish this.

 public void onDataChange(DataSnapshot dataSnapshot) {
                  for(DataSnapshot data: dataSnapshot.getChildren()){
                userinfo  user = data.getValue(userinfo.class);
                Log.d(TAG, "User name: " + user.getName() + ", mobile: " 
     + user.getMob() + ", Vehicle " + user.getVehicle() + ", Address " + 
     user.getAdd());}
            }
Enividmk
  • 41
  • 1
  • 4
  • https://stackoverflow.com/questions/44865530/why-i-am-not-able-to-reterive-all-the-user-details-from-database-in-firebase please look into this – Parth Tiwari Jul 01 '17 at 21:25