0

This question is not a duplicate of What is a NullPointerException, and how do I fix it? I have tried many of the answers given there but none worked!

I am trying to add WhatsApp like Recording and Slide to Cancel Animation in my app. For that i used : implementation 'com.github.3llomi:RecordView:3.0.2'. But the code is throwing a Null Pointer Exception:

(java.lang.NullPointerException: Attempt to invoke virtual method 'void com.devlomi.record_view.RecordView.onActionDown(com.devlomi.record_view.RecordButton, android.view.MotionEvent)' on a null object reference).

Can anyone help me solve this null object reference exception?

Java Code:

    binding.recordButton.setRecordView(binding.recordView);
binding.recordButton.setListenForRecord(false);
binding.recordView.setOnRecordListener(new OnRecordListener() {
        @Override
        public void onStart() {
            if (!checkPermissionFromDevice()) {
            
                binding.messageBox.setVisibility(View.GONE);
                binding.attachment.setVisibility(View.GONE);
                binding.camera.setVisibility(View.GONE);
                

                startRecord();
                Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                if (vibrator != null) {
                    vibrator.vibrate(100);
                }

            } else {
                requestPermission();
            }
        }

        @Override
        public void onCancel() {
            try {
                mediaRecorder.reset();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onFinish(long recordTime, boolean limitReached) {
           
            binding.messageBox.setVisibility(View.VISIBLE);
            binding.attachment.setVisibility(View.VISIBLE);
            binding.camera.setVisibility(View.VISIBLE);
            

            try {
                sTime = getHumanTimeText(recordTime);
                stopRecord();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

     
        @Override
        public void onLessThanSecond() {
            
            binding.messageBox.setVisibility(View.VISIBLE);
            binding.attachment.setVisibility(View.VISIBLE);
            binding.camera.setVisibility(View.VISIBLE);
            
        }
    });

    //binding.recordButton.setListenForRecord(false);

    //ListenForRecord must be false ,otherwise onClick will not be called
    binding.recordButton.setOnRecordClickListener(new OnRecordClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(ChatActivity.this, "RECORD BUTTON CLICKED", Toast.LENGTH_SHORT).show();
            Log.d("RecordButton","RECORD BUTTON CLICKED");
        }
    });

    binding.recordView.setOnBasketAnimationEndListener(new OnBasketAnimationEnd() {
        @Override
        public void onAnimationEnd() {
            
            binding.messageBox.setVisibility(View.VISIBLE);
            binding.attachment.setVisibility(View.VISIBLE);
            binding.camera.setVisibility(View.VISIBLE);
            
        }
    });

XML:

    <com.devlomi.record_view.RecordView
        android:id="@+id/record_view"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_margin="10dp"
        android:gravity="bottom"
        app:counter_time_color="#ff0000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/record_button"
        app:layout_constraintStart_toStartOf="parent"
        app:slide_to_cancel_arrow="@drawable/recv_ic_arrow"
        app:slide_to_cancel_arrow_color="@color/black"
        app:slide_to_cancel_bounds="8dp"
        app:slide_to_cancel_margin_right="10dp"
        app:slide_to_cancel_text="Slide To Cancel" />

    <com.devlomi.record_view.RecordButton
        android:id="@+id/record_button"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:layout_margin="6dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/recv_bg_mic"
        android:scaleType="centerInside"
        android:visibility="visible"
        android:padding="17dp"
        app:layout_constraintBottom_toBottomOf="@+id/record_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/record_view"
        app:mic_icon="@drawable/recv_ic_mic_white" />

Logcat:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.devlomi.record_view.RecordView.onActionDown(com.devlomi.record_view.RecordButton, android.view.MotionEvent)' on a null object reference
    at com.devlomi.record_view.RecordButton.onTouch(RecordButton.java:105)
    at android.view.View.dispatchTouchEvent(View.java:14441)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3170)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2775)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3170)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2775)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3170)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2775)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3170)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2775)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3170)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2775)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3170)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2775)
    at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:575)
    at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1945)
    at android.app.Activity.dispatchTouchEvent(Activity.java:4232)
    at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
    at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:528)
    at android.view.View.dispatchPointerEvent(View.java:14723)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6463)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:6246)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5707)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5774)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5735)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5897)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5743)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5954)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5711)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5774)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5735)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5743)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5711)
    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8551)
    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8502)
    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8454)
    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8691)
    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:238)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:339)
    at android.os.Looper.loop(Looper.java:208)
    at android.app.ActivityThread.main(ActivityThread.java:8212)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1015)

0 Answers0