I have this error in Android Studio when I am trying to access ProfileFragment.I'm missing something, that's why I ask you to look over this. I want to mention that in User and UserAccountSetting classes are only Get and Set methods, to get and set all data member like name, description etc
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.food.User.User.getUsername()' on a null object reference
at com.example.food.Utils.FirebaseMethods.getUserSettings(FirebaseMethods.java:274)
at com.example.food.Profile.ProfileFragment$4.onDataChange(ProfileFragment.java:181)
and my FirebaseMethod looks like this :
/* Retreive the accout settinigs for the user currlently logged in
* Database:user"_account_settings node
* @param dataSnapshot
* @return
*/
public UserSettings getUserSettings(DataSnapshot dataSnapshot){
Log.d(TAG, "getUserAccountSettings: retrieving user account settings from firebase");
UserAccountSettings settings = new UserAccountSettings();
User user = new User();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
//user account settings node
if (ds.getKey().equals(mContext.getString(R.string.dbname_user_account_settings))) {
Log.d(TAG, "getUserAccountSettings: datasnapshot" + ds);
try {
settings.setDisplay_name(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getDisplay_name()
);
settings.setUsername(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getUsername()
);
settings.setWebsite(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getWebsite()
);
settings.setDescription(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getDescription()
);
settings.setProfile_photo(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getProfile_photo()
);
settings.setPosts(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getPosts()
);
settings.setFollowing(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getFollowing()
);
settings.setFollowers(
ds.child(userID)
.getValue(UserAccountSettings.class)
.getFollowers()
);
} catch (NullPointerException e) {
Log.d(TAG, "getUserAccountSettings: NULLPointerException: " + e.getMessage());
}
Log.e(TAG, "getUserAccountSettings: retrieved user_account_settings information: " + settings.toString());
}
//users node
if (ds.getKey().equals(mContext.getString(R.string.dbname_users))) {
Log.d(TAG, "getUserAccountSettings: datasnapshot: " + ds);
user.setUsername(
ds.child(userID)
.getValue(User.class)
.getUsername() // here is line 274
);
user.setEmail(
ds.child(userID)
.getValue(User.class)
.getEmail()
);
user.setPhone_number(
ds.child(userID)
.getValue(User.class)
.getPhone_number()
);
user.setUser_id(
ds.child(userID)
.getValue(User.class)
.getUser_id()
);
Log.d(TAG, "getUserAccountSettings: retrieved users information: " + user.toString());
}
}
return new UserSettings(user, settings);
}
and ProfileFragment :
public class ProfileFragment extends Fragment {
private static final String TAG = "ProfileFragment";
private static final int ACTIVITY_NUM = 4;
private TextView mPosts, mFollowers, mFollowing, mDisplayName, mUsername, mWebsite, mDescription;
private CircleImageView mProfilePhoto;
private GridView gridView;
private Toolbar toolbar;
private AppCompatButton profileMenu, chat, follow_button;
private ChipNavigationBar chipNavigationBar;
private Context mContext;
//firebase
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;
private FirebaseMethods mFirebaseMethods;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
mDisplayName = (TextView) view.findViewById(R.id.display_name);
mUsername = (TextView) view.findViewById(R.id.username);
mWebsite = (TextView) view.findViewById(R.id.website);
mDescription = (TextView) view.findViewById(R.id.description);
mProfilePhoto = (CircleImageView) view.findViewById(R.id.profile_photo);
mPosts = (TextView) view.findViewById(R.id.tvPosts);
mFollowers = (TextView) view.findViewById(R.id.tvFollowers);
mFollowing = (TextView) view.findViewById(R.id.tvFollowing);
gridView = (GridView) view.findViewById(R.id.gridView);
toolbar = (Toolbar) view.findViewById(R.id.profileToolBar);
profileMenu = (AppCompatButton) view.findViewById(R.id.buton_setari);
chat = (AppCompatButton) view.findViewById(R.id.buton_mesaj);
follow_button = (AppCompatButton) view.findViewById(R.id.buttonFollow);
chipNavigationBar = (ChipNavigationBar) view.findViewById(R.id.navBar);
mContext = getActivity();
mFirebaseMethods = new FirebaseMethods(getActivity());
Log.d(TAG, "onCreateView: started");
private void setProfileWidgets(UserSettings userSettings){
Log.d(TAG, "setProfileWidgets: setting widgets with data retrieving from firebase databse: " + userSettings.toString());
UserAccountSettings settings = userSettings.getSettings();
UniversalImageLoader.setImage(settings.getProfile_photo(), mProfilePhoto, null, "");
mDisplayName.setText(settings.getDisplay_name());
mUsername.setText(settings.getUsername());
mWebsite.setText(settings.getWebsite());
mDescription.setText(settings.getDescription());
}
private void setupFirebaseAuth(){
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
//retrieve user info from database
setProfileWidgets(mFirebaseMethods.getUserSettings(snapshot)); // here is line 181
//retrieve image for the user in question
}
@Override
public void onCancelled(DatabaseError error) {
}
});
}
I looked over these too many times and I don't see anything wrong. Can you help me, please?:)