简体   繁体   中英

Unable to resolve symbol storageRef in firebase

I am using fire base to store the images in that i am getting a error in storage Ref unable to resolve the symbol please help.

public class Sample extends Activity {
public static final String GridViewDemo_ImagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pencilrulerlearner/";
StorageReference riversRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    File targetDirector = new File(GridViewDemo_ImagePath);
    File[] files = targetDirector.listFiles();
    for (File file1 : files) {
        Uri file= Uri.fromFile(file1);


        storageRef.child("images/" + file.getLastPathSegment());
      UploadTask  uploadTask = riversRef.putFile(file);


        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle unsuccessful uploads
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
                Uri downloadUrl = taskSnapshot.getDownloadUrl();
            }
        });
    }

}
}

Even i changed the storageRef to riversRef where i am getting the following error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.storage.StorageReference com.google.firebase.storage.StorageReference.child(java.lang.String)' on a null object reference
        at nidhinkumar.firebaseexample.Sample.onCreate(Sample.java:31)
        at android.app.Activity.performCreate(Activity.java:6092)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)

You're getting this error because storageRef variable hasn't been initialized. Any attempt to invoke a method on a null reference will lead to java.lang.NullPointerException .

In your case you should first get a reference to the FirebaseStorage instance:

FirebaseStorage storage = FirebaseStorage.getInstance();

Then, you can get a reference to the storage you want, by getReferenceFromUrl method.

StorageReference storageRef = storage.getReferenceFromUrl("Url to storage");

Hereinafter storageRef is initilized and can be used.

The variable storageRef is not initialized.

First, create a FirebaseStorage variable:

storage = FirebaseStorage.getInstance();

Then initialize the StorageReference type variable storageRef like this:

storageRef = storage.getReference();

StorageReference storageReference = FirebaseStorage.getInstance().getReference();

do jus a simple thing import this file

import com.google.firebase.storage.StorageReference ;

Your Welcome :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM