Munem Sohan
Munem Sohan

Reputation: 51

Bitmap Compress : Null point exception error

I'm getting a null pointer exception when trying to compress a bitmap so I can send it to a ByteArrayOutputStream to get a byte array. The log error is shown below.

java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                             at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                                             at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                             at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                             at java.lang.Thread.run(Thread.java:818)
                                                                          Caused by: 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 worldnp.zpmobi.mobi.test.MainActivity$Encode_image.doInBackground(MainActivity.java:102)
                                                                             at worldnp.zpmobi.mobi.test.MainActivity$Encode_image.doInBackground(MainActivity.java:94)
                                                                             at android.os.AsyncTask$2.call(AsyncTask.java:295)

java code :

protected Void doInBackground(Void... voids) {

            bitmap = BitmapFactory.decodeFile(file_uri.getPath());
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            bitmap.recycle();

            byte[] array = stream.toByteArray();
            encoded_string = Base64.encodeToString(array, 0);
            return null;
        }

when the bitmap is supposed to be compressed, the app is crashed. Where have i done wrong in my code ?

Upvotes: 0

Views: 891

Answers (1)

aliaksei
aliaksei

Reputation: 734

protected Void doInBackground(Void... voids) {

        Bitmap bitmap = BitmapFactory.decodeFile(file_uri.getPath());
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        bitmap.recycle();

        byte[] array = stream.toByteArray();
        encoded_string = Base64.encodeToString(array, 0);
        return null;
    }

You didn't give the variable bitmap a type when you initialised it.

Upvotes: 1

Related Questions