Reputation: 61
I have my android project where there are two different user types: an admin and a normal user. I actually found a solution here not specifically a solution since it does not work for me.
Home Fragment.java
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
final String uid = user.getUid();
Query userQuery = FirebaseDatabase.getInstance().getReference().child("User").child(uid).child("isAdmin");
userQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String type = dataSnapshot.child("isAdmin").getValue().toString();
if(type.equals("true")){
loadMenuBeachAdmin();
FloatingActionButton fab = (FloatingActionButton) homeView.findViewById(R.id.fab);
fab.show();
}
}
Database
The error I'm getting:
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String java.lang.Object.toString()' on a null object reference at
com.example.batangasbeachhousesfinal.HomeFragment$1.onDataChange(HomeFragment.java:92)
which is the String type = dataSnapshot.child("isAdmin").getValue().toString();
Upvotes: 0
Views: 67
Reputation: 80914
Change this:
Query userQuery = FirebaseDatabase.getInstance().getReference().child("User").child(uid).child("isAdmin");
userQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String type = dataSnapshot.child("isAdmin").getValue().toString();
if(type.equals("true")){
loadMenuBeachAdmin();
FloatingActionButton fab = (FloatingActionButton) homeView.findViewById(R.id.fab);
fab.show();
}
}
into this:
Query userQuery = FirebaseDatabase.getInstance().getReference().child("User").child(uid);
userQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String type = dataSnapshot.child("isAdmin").getValue().toString();
if(type.equals("true")){
loadMenuBeachAdmin();
FloatingActionButton fab = (FloatingActionButton) homeView.findViewById(R.id.fab);
fab.show();
}
}
Your dataSnapshot is already at child isAdmin
, so you need to go one step up and then you will be able to retrieve the isAdmin
child.
Upvotes: 1