Why am I encountering a null variable here?

I had used this tutorial in guiding me in developing an OpenCV app using Java on Android Studio that would be able to process frames for object recognition. As I have finished, I have encountered an error; the dreaded NullPointerException us Java programmers face.

The variable “net” in “net.setInput(blob);” was where the NullPointerException was saying was the problem. Traced back to initialization, where “net =”, the variable “Dnn.readNetFromCaffe(proto, weights)” might be considered to be null. If that’s the case, what would one do if Dnn.readNetFromCaffe is null?

also, the tutorial states that “MobileNetSSD_deploy.caffemodel” should be used. However, the most recent download of MobileNet only has “mobilenet_iter_73000.caffemodel”, which calls my claims for accuracy of this document into question.

This is the only error standing in between me and the completion of my project and any help would be a godsend.

EDIT: a link to the document: https://docs.opencv.org/4.0.1/d0/d6c/tutorial_dnn_android.html



    public void onCameraViewStarted(int width, int height)
        String proto = getPath("MobileNetSSD_deploy.prototxt", this);
        String weights = getPath("mobilenet_iter_73000.caffemodel", this);
        net = Dnn.readNetFromCaffe(proto, weights);
        Log.i(TAG, "Network loaded successfully");


    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
        mRGBA = inputFrame.rgba();

        final int IN_WIDTH = 300;
        final int IN_HEIGHT = 300;
        final float WH_RATIO = (float)IN_WIDTH / IN_HEIGHT;
        final double IN_SCALE_FACTOR = 0.007843;
        final double MEAN_VAL = 127.5;
        final double THRESHOLD = 0.2;
        Mat frame = inputFrame.rgba();
        Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2RGB);
        Mat blob = Dnn.blobFromImage(frame, IN_SCALE_FACTOR,
                new Size(IN_WIDTH, IN_HEIGHT),
                new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), /*swapRB*/false, /*crop*/false);
        net.setInput(blob); <---------------------- **ERROR POINTS TO THIS LINE, WHICH IS LINE 145**
        Mat detections = net.forward();

        int cols = frame.cols();
        int rows = frame.rows();

        detections = detections.reshape(1, (int)detections.total() / 7);

        for (int i =0; i < detections.rows(); ++i) {
            double confidence = detections.get(i, 2)[0];
            if (confidence > THRESHOLD) {
                int classId = (int)detections.get(i, 1)[0];

                int left = (int)(detections.get(i, 3)[0] * cols);
                int top = (int)(detections.get(i, 4)[0] * rows);
                int right = (int)(detections.get(i, 5)[0] * cols);
                int bottom = (int)(detections.get(i, 6)[0] * rows);

                Imgproc.rectangle(frame, new Point(left, top), new Point(right, bottom),
                        new Scalar(0, 255, 0));
                String label = classNames[classId] + ": " + confidence;
                int[] baseLine = new int[1];
                Size labelSize = Imgproc.getTextSize(label, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, 1, baseLine);

                Imgproc.rectangle(frame, new Point(left, top - labelSize.height),
                        new Point(left + labelSize.width, top + baseLine[0]),
                        new Scalar(255, 255, 255), Imgproc.FILLED);

                Imgproc.putText(frame, label, new Point(left, top),
                        Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(0, 0, 0));
        return frame;


    private Net net;

this is pretty weird,

Dnn.readNet() (or it’s fromXXX siblings) should throw an exception on failure, halting your program, did you miss (or disable) that ?

(e.g. Net net = Dnn.readNetFromCaffe("nada","/nada"); should result in an exception like: error: (-2:Unspecified error) FAILED: fs.is_open(). Can't open "nada" in function 'ReadProtoFromTextFile')
please edit your post, and show some lines of code around that, so we can take a better look at your problem

I’ve updated my post with the required code. I’ve been following the tutorial fairly closely, so I do think I may have missed/disabled that unknowingly. While the program does show that it launches on my device, it crashes prematurely, providing a NullPointerException in the logcat.

1 Like