1

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)

            

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Abacus Rathu
  • 77
  • 1
  • 1
  • 12

4 Answers4

4

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.

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • i have initialized the storagereference but i am getting the following error: Caused by: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. at com.google.firebase.FirebaseApp.getInstance(Unknown Source) at com.google.firebase.FirebaseApp.getInstance(Unknown Source) at com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source) – Abacus Rathu Jul 30 '16 at 10:16
  • how i have to do it can u tell me – Abacus Rathu Jul 30 '16 at 10:19
  • @AbacusRathu Please have a look at http://stackoverflow.com/documentation/android/3924/firebase-storage/13664/firebase-storage-operations#t=20160730103142745917 – frogatto Jul 30 '16 at 10:37
  • @AbacusRathu: Look at this related question: http://stackoverflow.com/questions/37342403/firebaseapp-with-name-default-doesnt-exist – Bob Snyder Jul 30 '16 at 18:19
0

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();
Ehsan
  • 36
  • 4
0

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

-1

do jus a simple thing import this file

import com.google.firebase.storage.StorageReference;

Your Welcome :)