Hot questions for Using Glide in android activity

Top 10 Java Open Source / Glide / android activity

Question:

I am trying to load images using the Glide Library from firebase. The image url is correct(Checked it using Logcat) . I am using glide in an activity and not a fragment. I am getting this error java.lang.NullPointerException: You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed).

Here is my code:

public class ProfileActivity extends AppCompatActivity {

FirebaseAuth auth;
DatabaseReference databaseReference;
TextView nameTextView , bioTextView;
String name2 , bio2 , uidString;
Uri imageUri , FilePathUri;
Button logOut , editProfile;
private SlidrInterface slidr;
CircleImageView profilepic;
StorageReference storageReference;
String Storage_Path = "ProfilePictures/" , downloadUrl;
Context context;

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

    auth = FirebaseAuth.getInstance();
    databaseReference = FirebaseDatabase.getInstance().getReference();
    final FirebaseUser user = auth.getCurrentUser();
    nameTextView = (TextView) findViewById(R.id.name);
    bioTextView = (TextView) findViewById(R.id.bio);
    logOut = (Button) findViewById(R.id.logout2);
    editProfile = (Button) findViewById(R.id.editprofile);
    slidr = Slidr.attach(this);
    profilepic = (CircleImageView) findViewById(R.id.profilepic);
    storageReference = FirebaseStorage.getInstance().getReference();

    Intent intent = getIntent();
    Bundle uid = intent.getExtras();
    uidString = uid.getString("UID");
    Log.d("UID OF USER" , "UID OF USER = "+uidString);

    final Context finalContext = context;
    databaseReference.child("users").child(uidString).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            name2 = (String) dataSnapshot.child("name").getValue();
            bio2 = (String) dataSnapshot.child("bio").getValue();


            String gender = (String) dataSnapshot.child("gender").getValue();
            String email = (String) dataSnapshot.child("email").getValue();

            Log.d("TAG", "Name: " + name2);
            Log.d("TAG", "Email: " + email);
            Log.d("TAG", "Gender: " + gender);
            Log.d("TAG", "Profile Pic URI = " + imageUri);
            nameTextView.setText(name2);
            bioTextView.setText(bio2);


            storageReference.child("ProfilePictures").child(uidString+".jpg");
            String imageUrl = (String) dataSnapshot.child("profilePic").getValue();
            GlideApp.with(context).load(imageUrl).into(profilepic);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}

    });


    logOut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences settings = getSharedPreferences(SignIn.PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("hasLoggedIn", false);
            editor.commit();
            boolean hasLoggedIn = settings.getBoolean("hasLoggedIn", false);
            Intent intent = new Intent(ProfileActivity.this, SignIn.class);
            startActivity(intent);
        }
    });

    editProfile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(ProfileActivity.this, EditProfile.class);
            startActivity(intent);
        }
    });

    Log.d("TAG" , "NAME OUTSIDE " +name2);



}
} 

If im not using using fragments why am I getting this error.

Also how will I solve this error. Please help me


Answer:

From the log, it is possible that your context is null. Try using activity directly like this

GlideApp.with(ProfileActivity.this).load(imageUrl).into(profilepic);