In my Android app, I have a database repository to contact Firebase Realtime Database and a service layer with some methods. My SaveUserProfile method works but I keep getting a null pointer on my GetUserFromUid.
The 'user' object it returns is null and I don't know why. The node in my DB is called "users" (all lowercase) and I want to retrieve a user as a model via their userId and display the name and email onscreen.
Can anybody see where I'm going wrong?
My DbContext:
public class DbContext implements IDbContext {
User user = null;
Context context;
DatabaseReference databaseUsers = FirebaseDatabase.getInstance().getReference("users");
public DbContext(Context context){
super();
this.context = context;
}
#Override
public void AddUserAccount(User user1) {
databaseUsers.child(user1.userId).setValue(user1);
}
#Override
public User GetUserFromFirebase(String uid) {
databaseUsers.child(uid)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
user = dataSnapshot.getValue(User.class);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
return user;
}
My DbService:
public class DbService implements IDbService {
//instance of DbContext for firebase handling
private DbContext dbContext;
public DbService(Context context){
super();
dbContext = new DbContext(context);
}
#Override
public User SaveUserProfile(User u) {
dbContext.AddUserAccount(u);
return u;
}
#Override
public User GetUserFromUid(String uid) {
User user = dbContext.GetUserFromFirebase(uid);
return user;
}
My User model:
public class User {
public String userId;
public String name;
public String email;
public String account;
//constructor required for calls to DataSnapshot.getValue(User.class)
public User(){
}
public User(String userId, String name, String email, String account) {
this.userId = userId;
this.name = name;
this.email = email;
this.account = account;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
My activity where I want to display the users' details:
public class DetailsActivity extends AppCompatActivity {
//tag
private static final String TAG = DetailsActivity.class.getSimpleName();
//firebase auth
private FirebaseAuth mAuth;
//variables
private TextView inputName, inputEmail;
private DatabaseReference mFirebaseDatabase;
private String userId;
public String currentUserAccount;
public String teacherAccountNav = "Teacher";
public User userDetails;
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
inputName = findViewById(R.id.nameTextView);
inputEmail = findViewById(R.id.emailTextView);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
assert user != null;
userId = user.getUid();
getUserDetails(userId);
inputName.setText(userDetails.name);
inputEmail.setText(userDetails.email);
}
public User getUserDetails(String uid){
DbService dbService = new DbService(this);
userDetails = dbService.GetUserFromUid(uid);
return userDetails;
}
EDITS BELOW
My callback:
public interface Callback {
void myResponseCallback(User user);
}
My EDITED DbContext:
public class DbContext implements IDbContext {
User user = null;
Context context;
DatabaseReference databaseUsers = FirebaseDatabase.getInstance().getReference("users");
public DbContext(Context context) {
this.context = context;
}
#Override
public void AddUserAccount(User user1) {
databaseUsers.child(user1.userId).setValue(user1);
}
#Override
public void GetUserFromFirebase(String uid, final Callback callback) {
databaseUsers.child(uid)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
user = dataSnapshot.getValue(User.class);
callback.myResponseCallback(user);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
My EDITED DbService:
public class DbService implements IDbService {
//instance of DbContext for firebase handling
public DbContext dbContext;
public DbService(Context context){
super();
dbContext = new DbContext(context);
}
#Override
public User SaveUserProfile(User u) {
dbContext.AddUserAccount(u);
return u;
}
#Override
public User GetUserFromUid(String uid) {
final User[] user1 = {new User()};
dbContext.GetUserFromFirebase(uid, new Callback() {
#Override
public void myResponseCallback(User user) {
user1[0] = user;
}
});
return user1[0];
}
My EDITED Activity:
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
inputName = findViewById(R.id.nameTextView);
inputEmail = findViewById(R.id.emailTextView);
FirebaseDatabase mFirebaseInstance = FirebaseDatabase.getInstance();
//reference to 'users' node
mFirebaseDatabase = mFirebaseInstance.getReference("users");
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
assert user != null;
userId = user.getUid();
getUserFromFirebase(userId);
}
public void getUserFromFirebase(String uid){
userDetails = (new DbService(this)).GetUserFromUid(uid);
inputEmail.setText(userDetails.email);
inputName.setText(userDetails.name);
}
Debug BEFORE edits:
Debug AFTER edits:
As you can see from the debug, before the callback interface, userDetails was null. After the interface implementation, userDetails is not null but all of the object values are null. I don't know why this is as they are filled in the database. Any ideas?
Related
I'm trying to build a ticket booking app for an event and I managed to do the registration and log-in part. After the login, the user profile shows up which contains some TextViews about the name, age, email, etc. and I want to read the data from the real-time database and put it in TextView.I don't know what is the problem but the TextViews won't show the data.
User profile class
public class ProfilUser extends AppCompatActivity {
private FirebaseUser user;
private DatabaseReference referinta;
private String userID;
private Button delogare;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profil_user);
delogare = (Button) findViewById(R.id.delogare);
delogare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(ProfilUser.this,MainActivity.class));
}
});
user = FirebaseAuth.getInstance().getCurrentUser();;
userID = user.getUid();
referinta = FirebaseDatabase.getInstance("https://rezervarebileteveniment-default-rtdb.europe-west1.firebasedatabase.app").getReference("Users").child(userID);
TextView bunVenitTextView = (TextView) findViewById(R.id.bunVenit);
TextView numeTextView = (TextView) findViewById(R.id.getNume);
TextView prenumeTextView = (TextView) findViewById(R.id.getPrenume);
TextView varstaTextView = (TextView) findViewById(R.id.getVarsta);
TextView emailTextView = (TextView) findViewById(R.id.getAdresaEmail);
referinta.child(userID).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
User profilUser = snapshot.getValue(User.class);
if(profilUser != null){
String nume = profilUser.nume;
String prenume = profilUser.prenume;
String email = profilUser.email;
String varsta = profilUser.varsta;
bunVenitTextView.setText("Buna ziua, "+nume + " "+prenume +"!");
numeTextView.setText(nume);
prenumeTextView.setText(prenume);
emailTextView.setText(email);
varstaTextView.setText(varsta);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
Toast.makeText(ProfilUser.this,"Ceva nu a mers bine!", Toast.LENGTH_LONG).show();
}
});
}
}
User class
package oct.alx.rezervarebileteveniment;
public class User {
public String nume, prenume , varsta, email;
public User(){
}
public User(String nume, String prenume, String varsta, String email){
this.nume = nume;
this.prenume = prenume;
this.varsta = varsta;
this.email = email;
}
public String getNume(){
return nume;
}
public void setNume(String nume){
this.nume = nume;
}
public String getPrenume() {
return prenume;
}
public void setPrenume(String prenume) {
this.prenume = prenume;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getVarsta() {
return varsta;
}
public void setVarsta(String varsta) {
this.varsta = varsta;
}
}
The problem in your code lies in the fact that you're calling .child(userID) twice, once when you create the reference:
referinta = FirebaseDatabase.getInstance("https://rezervarebileteveniment-default-rtdb.europe-west1.firebasedatabase.app")
.getReference("Users")
.child(userID); //👈
And second time when you attach the listener:
referinta.child(userID).addListenerForSingleValueEvent(/*... /*);
// 👆
This means that you're trying to read the data from a location that doesn't exist. To solve this, you either remove that method call from the first line of code or from the second one.
P.S. To use encapsulation, try to make all fields in your class private:
private String nume, prenume , varsta, email;
public class cp extends AppCompatActivity {
ListView ListPdf;
DatabaseReference databaseReference;
List<upload> uploadPdf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cp);
ListPdf = (ListView) findViewById(R.id.List);
uploadPdf = new ArrayList<>();
ViewAllPdf();
}
private void ViewAllPdf() {
databaseReference = FirebaseDatabase.getInstance().getReference("year_1");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot PostSnapShot: dataSnapshot.getChildren()){
upload Pdf = PostSnapShot.getValue(upload.class);
uploadPdf.add(Pdf);
}
String[] uploads = new String[uploadPdf.size()];
for(int i=0;i<uploads.length;i++){
uploads[i] = uploadPdf.get(i).getName();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,uploads);
ListPdf.setAdapter(adapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
upload class
public class upload {
public String name;
public upload() {
this.name = name;
}
public String getName() {
return name;
}
}
In uploads[ ] it is storing null instead of file name,i cannot figure out what's the problem. I tried logcat for debuging but it shows Pdf object has three files but in uploads[ ] it stores NULL. Please look into it
Firebase database screenshot
The problem with your code is your POJO class Upload. See an example from Firebase Documentation
public class User {
public String username;
public String email;
public User() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public User(String username, String email) {
this.username = username;
this.email = email;
}
}
Also the fields name in your Realtime Database doesn't match with your POJO class.
I am trying to get a hang of the Android Architecture Library and i have been trying to display texts from retrofit via a ViewModel using Mutable LiveData on my Main Activity but i cant seem to do it, i would really appreciate some assistance.
Here is my Model Class
public class User {
#SerializedName("name")
#Expose
private String name;
#SerializedName("email")
#Expose
private String email;
#SerializedName("phone")
#Expose
private String phone;
public User() {
}
public String getUserName() {
return name;
}
public void setUserName(String name) {
this.name = name;
}
public String getUserEmail() {
return email;
}
public void setUserEmail(String email) {
this.email = email;
}
public String getUserPhone() {
return phone;
}
public void setUserPhone(String phone) {
this.phone = phone;
}
}
My View model
public class UserViewModel extends AndroidViewModel {
private NodeAuthService api;
private SharedPreferences pref;
private static MutableLiveData<List<User>> userDetails = new
MutableLiveData<>();
private Call<List<User>> call;
public UserViewModel(#NonNull Application application) {
super(application);
api = AuthRetrofitClient.getInstance().create(NodeAuthService.class);
}
private String email = pref.getString("email", "");
public void loadUser(){
call = api.getUser(email);
call.enqueue(new Callback<List<User>>() {
#Override
public void onResponse(Call<List<User>> call, Response<List<User>>
response) {
List<User> users = response.body();
}
#Override
public void onFailure(Call<List<User>> call, Throwable t) {
Log.d("USER",t.getMessage());
}
});
}
public MutableLiveData<List<User>> getUserDetails(){
return userDetails;
}
}
Simplified version of my MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView navName = (TextView) findViewById(R.id.navigation_name);
TextView navEmail = (TextView) findViewById(R.id.navigation_email);
}
Kindly assist
In your viewModel
public void loadUser(){
call = api.getUser(email);
call.enqueue(new Callback<List<User>>() {
#Override
public void onResponse(Call<List<User>> call, Response<List<User>>
response) {
userDetails.postValue(response.body())
}
#Override
public void onFailure(Call<List<User>> call, Throwable t) {
Log.d("USER",t.getMessage());
}
});
}
And in your Activity observe the changes
yourVm.getUserDetails().observe(this, models -> {
if (models != null) {
for(int = 0; i<models.size();i++){
/*you will get your list here now iterate through list and get
your email_id here.*/
}
}
});
instead of this
List<User> users = response.body();
use this
userDetails.postValue(response.body())
also, remove static before livedata
inside activity oncreate() init your viewModel instance.
then viewModel.getUserDEtails().obsever(this, data-> // your stuff);
I struggle to get data from Firebase for Android.
Please see my code.
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Logger.log("onChildAdded:" + previousChildName);
// Logger.log( dataSnapshot.child("topicid").getValue(String.class));
//dataSnapshot.child("topicid").getValue();
mComment = new ArticleComment();
mComment = dataSnapshot.getValue(ArticleComment.class);
}
Here is ArticleComment class
public class ArticleComment {
private Date mCommentTime;
private String mComment;
private String mUID;
private String mName;
private int mColorRed;
private int mColorGreen;
private int mColorBlue;
public ArticleComment(){
}
public ArticleComment(Date time, String comment,String name,String uid,int[] color){
this.mCommentTime = time;
this.mComment = comment;
this.mName = name;
this.mUID = uid;
this.mColorRed=color[0];
this.mColorGreen=color[1];
this.mColorBlue=color[2];
}
public Date getTime(){
return mCommentTime;
}
public void setTime(Date time){
mCommentTime =time;
}
public String getComment(){
return mComment;
}
public void setComment(String comment){
mComment =comment;
}
public String getName(){
return mName;
}
public void setName(String name){
mName=name;
}
public String getUID(){
return mUID;
}
public void setUID(String uid){
mUID=uid;
}
public int getColorRed(){
return mColorRed;
}
public int getColorGreen(){
return mColorGreen;
}
public int getColorBlue(){
return mColorBlue;
}
public void setColor(int red,int green, int blue)
{
mColorRed=red;
mColorGreen=green;
mColorBlue=blue;
}
#Exclude
public Map<String, Object> toMap(){
HashMap<String, Object> hashmap = new HashMap<>();
hashmap.put("time", mCommentTime);
hashmap.put("UID", mUID);
hashmap.put("name", mName);
hashmap.put("comment", mComment);
hashmap.put("colorRed", mColorRed);
hashmap.put("colorBlue", mColorBlue);
hashmap.put("colorGreen", mColorGreen);
return hashmap;
}
}
And here is my DB information.
I could get only
mCommentTime;
mComment;
mName;
But I can't get
mUID;
mColorRed;
mColorGreen;
mColorBlue;
Is there something wrong with my code?
Actually datasnapshot has data but it didn't copy to mComment
Hi! Thank you, friends, gave me feedback here is all code.
public class CommentsActivity extends AppCompatActivity {
private DatabaseReference myRef;
private DatabaseReference mTopicRef;
private RecyclerView mRecyclerView;
private RecyclerArticleAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<ArticleComment> myDataset = new ArrayList<ArticleComment>();
private ArticleComment mComment;
private Topic mTopic;
private EditText mTitleEditText;
private String mUserName;
private String mUid;
private int mRedColor=100;
private int mBlueColor=100;
private int mGreenolor=100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
//Add action button
Intent intent =getIntent();
String id = intent.getStringExtra("ID");
String date = intent.getStringExtra("Date");
String title = intent.getStringExtra("Title");
String topic = intent.getStringExtra("Topic");
mUserName = intent.getStringExtra("UserName");
mUid = intent.getStringExtra("UID");
SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(this);
mRedColor= pref.getInt("ColorRed",255);
mGreenolor=pref.getInt("ColorGreen",255);
mBlueColor=pref.getInt("ColorBlue",255);
mRecyclerView = findViewById(R.id.recyclerView_article);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
//Reference DB
FirebaseDatabase database = FirebaseDatabase.getInstance();
myRef = database.getReference("Main").child("Comments").child(id);
mTopicRef = database.getReference("Main").child("Topics").child(id);
List<String> topicinfo=new ArrayList<String >();
topicinfo.add(date);
topicinfo.add(title);
topicinfo.add(topic);
// Set TestAdapter as the adapter for RecyclerView.
mRecyclerView.setAdapter(mAdapter);
mAdapter = new RecyclerArticleAdapter(myDataset,topicinfo){
/* #Override
protected void onCheckedChangedRecycle(CompoundButton comButton, final boolean isChecked){
mTopicRef.runTransaction(new Transaction.Handler() {
#Override
public Transaction.Result doTransaction(MutableData mutableData) {
Topic t = mutableData.getValue(Topic.class);
String id = mutableData.child("topicid").getValue(String.class);
t.setTopicID(id);
if (t == null) {
return Transaction.success(mutableData);
}
if (isChecked==true) {
// Unstar the post and remove self from stars
t.setRate(t.getRate()+1);
} else {
// Star the post and add self to stars
t.setRate(t.getRate()-1);
}
// Set value and report transaction success
mutableData.setValue(t);
return Transaction.success(mutableData);
}
#Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Transaction completed
}
});
} */
};
mRecyclerView.setAdapter(mAdapter);
findViewById(R.id.button2).setOnClickListener(button1ClickListener);
mTitleEditText = findViewById(R.id.editText2);
ChildEventListener childEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
mComment = new ArticleComment();
mComment = dataSnapshot.getValue(ArticleComment.class);
Logger.log("onChildAdded:addItem" + previousChildName);
mAdapter.addItem(mAdapter.getItemCount()-1, mComment);
Logger.log("onChildAdded:scrollToPosition" + previousChildName);
mAdapter.updateItem(mAdapter.getItemCount()-1,mComment);
mLayoutManager.scrollToPosition(mAdapter.getItemCount()-1);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
myRef.addChildEventListener(childEventListener);
}
View.OnClickListener button1ClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// finish();
Logger.log("onClick");
if(mTitleEditText.getText().toString().equals("")){
return;
}
//setting color
int color[]=new int[3];
color[0]= mRedColor;
color[1]=mGreenolor;
color[2]=mBlueColor;
mComment = new ArticleComment(new Date(),mTitleEditText.getText().toString(),mUserName,mUid,color);
sendTopic(mComment,myRef);
//Delete all text
mTitleEditText.setText("");
}
};
// Sending topic to DB
public void sendTopic(ArticleComment test,DatabaseReference ref) {
String key = ref.push().getKey();
Map<String, Object> map = new HashMap<>();
map.put(key, test.toMap());
ref.updateChildren(map);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return true;
}
Is it enough information?
Firebase follows JavaBean naming conventions to determine the name of the JSON property from the Java code. And in that convention getUID and setUID map to a property uID with a lowercase u.
To make the Firebase client adopt your naming convention, annotate the getter and setter with a #PropertyName:
#PropertyName("UID")
public String getUID(){
return mUID;
}
#PropertyName("UID")
public void setUID(String uid){
mUID=uid;
}
I'm not immediately sure why the other properties don't work. When this happens, I find it most useful to write an object of the type of Firebase, to see what it generates.
**
If you want to store values in a model .Then your variable name should
be same as Firebase node or else you need to typeConvert it .
**
private Date mCommentTime;
private String mComment;
private String mUID;
private String mName;
private int mColorRed;
private int mColorGreen;
private int mColorBlue;
Replace above with
private int time;
private String comment;
private String UID;
private String name;
private int colorRed;
private int colorBlue;
private int colorGreen;
now make getters,setters,constructor etc. using above nodes. (Now you will be able to get all the values ) .
Hope its gonna help you :)
I would like to list the users who registered to the system.
MainPage:
List<User> users;
databaseReference = FirebaseDatabase.getInstance().getReference("Users");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
{
for(DataSnapshot postSnapShot:dataSnapshot.getChildren())
{
User user = postSnapShot.getValue(User.class);
users.add(user);
customAdapter.notifyDataSetChanged();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("selam", "onCancelled: " + databaseError );
}
});
User Java Class
public class User {
private String email ="";
private String nickname="";
private String status="";
private String uid="";
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
Ä°mages
You get that NullPointerException because you haven't initialized your users list. To solve this, please use the following code:
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
{
List<User> users = new ArrayList<>(); //Initialize the list
for(DataSnapshot postSnapShot : dataSnapshot.getChildren())
{
User user = postSnapShot.child("userInfo").getValue(User.class);
users.add(user);
}
customAdapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("selam", "onCancelled: " + databaseError );
}
});
Please also note, that I moved customAdapter.notifyDataSetChanged();, outside the for loop and I have also added .child("userInfo") call because there is an extra level in your database tree.