Null Pointer Exception when using handler.obtainmessage

I am using a handler to pass the value of a variable to the UI thread in android. However, the application crashes as soon as it reaches the handler. Any idea to fix it would be a great help. Here is my code:

String var = "NO Keyword Found";
                        Message completeMessage = mHandler.obtainMessage();
                        completeMessage.obj = var;

                        completeMessage.sendToTarget();

      

Thank you in advance

Here is the complete threD code:

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

import android.util.Log;

public class FrameProcessor extends Thread {
    private Uri uri = null;
    private Handler mHandler = null;
    private static volatile boolean bKeepRunning = true;
    private String value = null;
    private Context context = null;

    public FrameProcessor(Object obj, Handler handler, Uri uri) {
        if (obj != null) {
            this.uri = uri;
            this.mHandler = handler;
        }
        this.context = (Context) obj;
    }

    public void setIndex(String val) {
        this.value = val;
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void run() {

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();

        retriever.setDataSource(uri.toString(), new HashMap<String, String>());
        Log.v("FrameProcessor", "Video path set.... ");
        ArrayList<Bitmap> rev = new ArrayList<Bitmap>();
        Log.v("FrameProcessor", "uri is: " + uri);
        MediaPlayer mp = MediaPlayer.create(context, uri);
        Log.v("FrameProcessor", "MP created ");
        int millis = mp.getDuration();

        for (int i = 1000000; i < millis * 1000; i += 1000000) {
            Log.v("FrameProcessor", "In Thread for loop ");
            Bitmap bitmap = retriever.getFrameAtTime(i,
                    MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
            rev.add(bitmap);

            try {
                saveFrames(rev);
            } catch (IOException e) {

                e.printStackTrace();
            }
        }

    }

    @SuppressLint("ShowToast")
    public void saveFrames(ArrayList<Bitmap> saveBitmapList) throws IOException {
        Log.v("FrameProcessor", "In saveFrames ");
        if (value != null) {
            Log.v("FrameProcessor", value);

            if (value.equals("0")) {
                uri = Uri
                        .parse("http://html5dev.anorakstech.com/videos/video.mp4");
            } else if (value.equals("1")) {
                uri = Uri
                        .parse("http://html5dev.anorakstech.com/videos/HSNvideo.mp4");
            } else if (value.equals("2")) {
                uri = Uri
                        .parse("http://html5dev.anorakstech.com/videos/HSNMarkVideo.mp4");
            }
            Log.v("FrameProcessor", uri.toString());
        } else {

        }
        if (value.equals("0")) {
            String folder = Environment.getExternalStorageDirectory()
                    .toString();

            File saveFolder = new File(folder + "/Movies/Frames/");

            if (!saveFolder.exists()) {
                saveFolder.mkdirs();

                Log.v("FrameProcessor", "Extracting Frames.... ");

            }

            int i = 1;
            for (Bitmap b : saveBitmapList) {
                Log.v("FrameProcessor", "In saveFrames for loop");
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                b.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

                File f = new File(saveFolder, ("frame" + i + ".jpg"));

                f.createNewFile();
                if (i >= 0 && i < 6) {

                    String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;

                    completeMessage.sendToTarget();
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else if (i >= 6 && i < 12) {

                    **String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;

                    completeMessage.sendToTarget();**
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else if (i >= 12 && i < 18) {
                    String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;
                    completeMessage.sendToTarget();
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else if (i > 18 && i < 24) {
                    String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;

                    completeMessage.sendToTarget();
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else {

                }
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());

                fo.flush();
                fo.close();

                i++;
            }
        } else if (value.equals("1")) {
            String folder = Environment.getExternalStorageDirectory()
                    .toString();

            File saveFolder = new File(folder + "/Movies/Frames/");

            if (!saveFolder.exists()) {
                saveFolder.mkdirs();

                Log.v("FrameProcessor", "Extracting Frames.... ");

            }

            int i = 1;
            for (Bitmap b : saveBitmapList) {
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                b.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

                File f = new File(saveFolder, ("frame" + i + ".jpg"));

                f.createNewFile();
                if (i >= 0 && i < 6) {

                    String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;

                    completeMessage.sendToTarget();
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else if (i >= 6 && i < 12) {

                    String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;

                    completeMessage.sendToTarget();
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else if (i >= 12 && i < 18) {
                    String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;

                    completeMessage.sendToTarget();
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else if (i > 18 && i < 24) {
                    String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;

                    completeMessage.sendToTarget();
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else {

                }
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());

                fo.flush();
                fo.close();

                i++;
            }
        } else {
            String folder = Environment.getExternalStorageDirectory()
                    .toString();

            File saveFolder = new File(folder + "/Movies/Frames/");

            if (!saveFolder.exists()) {
                saveFolder.mkdirs();

                Log.v("FrameProcessor", "Extracting Frames.... ");

            }

            int i = 1;
            while (bKeepRunning) {

                for (Bitmap b : saveBitmapList) {
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    b.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

                    File f = new File(saveFolder, ("frame" + i + ".jpg"));

                    f.createNewFile();
                    if (i >= 0 && i < 6) {

                        String var = "Keyword HSN Found";
                        Message completeMessage = mHandler.obtainMessage();
                        completeMessage.obj = var;

                        completeMessage.sendToTarget();
                        Log.v("FrameProcessor", "Keyword ABCFound");

                    } else if (i >= 6 && i < 12) {

                        String var = "Keyword HSN Found";
                        Message completeMessage = mHandler.obtainMessage();
                        completeMessage.obj = var;
                        Log.v("FrameProcessor", "Keyword SHOP Found");

                    } else if (i >= 12 && i < 18) {
                        String var = "Keyword HSN Found";
                        Message completeMessage = mHandler.obtainMessage();
                        completeMessage.obj = var;
                        Log.v("FrameProcessor", "Keyword BY Found");

                    } else if (i > 18 && i < 24) {
                        String var = "Keyword HSN Found";
                        Message completeMessage = mHandler.obtainMessage();
                        completeMessage.obj = var;
                        Log.v("FrameProcessor", "Keyword REMOTE Found");

                    } else {

                    }
                    FileOutputStream fo = new FileOutputStream(f);
                    fo.write(bytes.toByteArray());

                    fo.flush();
                    fo.close();

                    i++;
                }
            }
        }
    }

    public void kill() {
        bKeepRunning = false;
    }
}

      

+3


source to share


1 answer


If the handler is null, you can use

Message.obtain()

      



instead of this. In any case, you will need to check if a handler has been processed before sending the message.

+1


source







All Articles