0

I tried to upload an image while using the imageView in android and it did successfully uploaded the image. But when I used imageButton in the place of imageView I get the error.

imageButton.setDrawingCacheEnabled(true);
imageButton.buildDrawingCache();
Bitmap bitmap = imageButton.getDrawingCache();
ByteArrayOutputStream boas = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, boas);
imageButton.setDrawingCacheEnabled(false);
byte[] data = boas.toByteArray();

String path = "firememes/" + UUID.randomUUID() + ".png";
StorageReference firememRef = storage.getReference(path);

I am getting this error

07-19 17:17:17.154 21469-21469/com.umer.listview D/AndroidRuntime: Shutting down VM
07-19 17:17:17.154 21469-21469/com.umer.listview E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.umer.listview, PID: 21469
                                                                   java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
                                                                       at com.umer.listview.MainActivity$2.onClick(MainActivity.java:125)
                                                                       at android.view.View.performClick(View.java:5204)
                                                                       at android.view.View$PerformClick.run(View.java:21153)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:148)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Tim Jul 19 '17 at 12:28
  • I am getting error at this line of code bitmap.compress(Bitmap.CompressFormat.PNG, 100, boas); – Umer Abdul Jabbar Jul 19 '17 at 12:30
  • yes we know that. The error message says it – Tim Jul 19 '17 at 12:32

1 Answers1

0

Actually you're trying to set the image to a null object,that mean that android can't find the object in this case your ImageButton where to set the image got from the DB, that means that you have to initialize the activity where you have your button before you'll set the image to it from the firebase.

John K
  • 371
  • 5
  • 26